1 |
/* wptGPGUtil.cpp - GPG helper functions |
/* wptGPGUtil.cpp - GPG util functions |
2 |
* Copyright (C) 2005 g10 Code GmbH |
* Copyright (C) 2005 g10 Code GmbH |
3 |
|
* Copyright (C) 2005 Timo Schulz |
4 |
* |
* |
5 |
* This file is part of WinPT. |
* This file is part of WinPT. |
6 |
* |
* |
18 |
* along with WinPT; if not, write to the Free Software Foundation, |
* along with WinPT; if not, write to the Free Software Foundation, |
19 |
* Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA |
* Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA |
20 |
*/ |
*/ |
21 |
|
|
22 |
#ifdef HAVE_CONFIG_H |
#ifdef HAVE_CONFIG_H |
23 |
#include <config.h> |
#include <config.h> |
24 |
#endif |
#endif |
26 |
#include <windows.h> |
#include <windows.h> |
27 |
#include <sys/stat.h> |
#include <sys/stat.h> |
28 |
|
|
|
#include "wptErrors.h" |
|
29 |
#include "gpgme.h" |
#include "gpgme.h" |
30 |
|
#include "wptTypes.h" |
31 |
|
#include "wptErrors.h" |
32 |
|
|
33 |
#define NROFHEXDIGITS 2 |
#define NROFHEXDIGITS 2 |
34 |
/* Convert two hexadecimal digits from STR to the value they |
/* Convert two hexadecimal digits from STR to the value they |
154 |
len += 1; |
len += 1; |
155 |
p = (char*)calloc (1, len+1); |
p = (char*)calloc (1, len+1); |
156 |
if (!p) |
if (!p) |
157 |
abort (); |
BUG (NULL); |
158 |
len = ExpandEnvironmentStrings (path, p, len); |
len = ExpandEnvironmentStrings (path, p, len); |
159 |
if (!len) { |
if (!len) { |
160 |
free (p); |
free (p); |
182 |
goto leave; |
goto leave; |
183 |
result = (char*)calloc (1, (n1=nbytes+1)); |
result = (char*)calloc (1, (n1=nbytes+1)); |
184 |
if (!result) |
if (!result) |
185 |
abort (); |
BUG (NULL); |
186 |
if (RegQueryValueEx (key_handle, name, 0, &type, (BYTE*)result, &n1)) { |
if (RegQueryValueEx (key_handle, name, 0, &type, (BYTE*)result, &n1)) { |
187 |
free (result); |
free (result); |
188 |
result = NULL; |
result = NULL; |
206 |
create_tmpfile (const char *name) |
create_tmpfile (const char *name) |
207 |
{ |
{ |
208 |
HANDLE out; |
HANDLE out; |
209 |
SECURITY_ATTRIBUTES sattr; |
SECURITY_ATTRIBUTES sec_attr; |
210 |
char tmp[300]; |
char tmp[300]; |
211 |
|
|
212 |
memset (&sattr, 0, sizeof sattr); |
memset (&sec_attr, 0, sizeof sec_attr); |
213 |
sattr.bInheritHandle = TRUE; |
sec_attr.bInheritHandle = TRUE; |
214 |
sattr.lpSecurityDescriptor = NULL; |
sec_attr.lpSecurityDescriptor = NULL; |
215 |
sattr.nLength = sizeof sattr; |
sec_attr.nLength = sizeof sec_attr; |
216 |
|
|
217 |
GetTempPath (sizeof (tmp)-1 - strlen (name)-1, tmp); |
GetTempPath (sizeof (tmp)-1 - strlen (name)-1, tmp); |
218 |
strcat (tmp, name); |
strcat (tmp, name); |
219 |
out = CreateFile (tmp, GENERIC_READ|GENERIC_WRITE, |
out = CreateFile (tmp, GENERIC_READ|GENERIC_WRITE, |
220 |
FILE_SHARE_WRITE, &sattr, |
FILE_SHARE_WRITE, &sec_attr, |
221 |
OPEN_ALWAYS, FILE_FLAG_DELETE_ON_CLOSE, NULL); |
OPEN_ALWAYS, FILE_FLAG_DELETE_ON_CLOSE, NULL); |
222 |
|
if (out == INVALID_HANDLE_VALUE) |
223 |
|
log_debug ("create_tmpfile: CreateFile failed ec=%d\r\n", |
224 |
|
(int)GetLastError ()); |
225 |
return out; |
return out; |
226 |
} |
} |
227 |
|
|
240 |
sec_attr.bInheritHandle = TRUE; |
sec_attr.bInheritHandle = TRUE; |
241 |
sec_attr.nLength = sizeof sec_attr; |
sec_attr.nLength = sizeof sec_attr; |
242 |
|
|
243 |
if (!CreatePipe (&r, &w, &sec_attr, 4096)) |
if (!CreatePipe (&r, &w, &sec_attr, 4096)) { |
244 |
|
log_debug ("create_in_pipe: CreatePipeFailed ec=%d\r\n", |
245 |
|
(int)GetLastError ()); |
246 |
return NULL; |
return NULL; |
247 |
|
} |
248 |
|
|
249 |
WriteFile (w, dat, strlen (dat), &n, NULL); |
WriteFile (w, dat, strlen (dat), &n, NULL); |
250 |
CloseHandle (w); |
CloseHandle (w); |
266 |
n = GetFileSize (out, NULL); |
n = GetFileSize (out, NULL); |
267 |
p = (char*)calloc (1, n+1); |
p = (char*)calloc (1, n+1); |
268 |
if (!p) |
if (!p) |
269 |
abort (); |
BUG (NULL); |
270 |
ReadFile (out, p, n, &n, NULL); |
ReadFile (out, p, n, &n, NULL); |
271 |
p[n] = 0; |
p[n] = 0; |
272 |
return p; |
return p; |
287 |
memset (&si, 0, sizeof (si)); |
memset (&si, 0, sizeof (si)); |
288 |
si.cb = sizeof si; |
si.cb = sizeof si; |
289 |
if (in || out) |
if (in || out) |
290 |
si.dwFlags = STARTF_USESTDHANDLES|STARTF_USESHOWWINDOW; |
si.dwFlags = STARTF_USESTDHANDLES; |
291 |
if (out) |
if (out) |
292 |
si.hStdOutput = out; |
si.hStdOutput = out; |
293 |
if (in) |
if (in) |
294 |
si.hStdInput = in; |
si.hStdInput = in; |
295 |
|
si.dwFlags |= STARTF_USESHOWWINDOW; |
296 |
si.wShowWindow = SW_HIDE; |
si.wShowWindow = SW_HIDE; |
297 |
if (!CreateProcess (NULL, (char*)cmd, NULL, NULL, TRUE, 0, |
if (!CreateProcess (NULL, (char*)cmd, NULL, NULL, TRUE, 0, |
298 |
NULL, NULL, &si, &pi)) { |
NULL, NULL, &si, &pi)) { |
299 |
log_debug ("create_process: CreateProcess() failed ec=%d\r\n", |
log_debug ("create_process: CreateProcess failed ec=%d\r\n", |
300 |
(int)GetLastError ()); |
(int)GetLastError ()); |
301 |
return -1; |
return -1; |
302 |
} |
} |
323 |
cmd = (char*)calloc (1, strlen (p) + strlen (keyid) |
cmd = (char*)calloc (1, strlen (p) + strlen (keyid) |
324 |
+ strlen (outfile) + 64 + 2); |
+ strlen (outfile) + 64 + 2); |
325 |
if (!cmd) |
if (!cmd) |
326 |
abort (); |
BUG (NULL); |
327 |
sprintf (cmd, "%s --yes --output \"%s\" --export-secret-key %s", |
sprintf (cmd, "%s --yes --output \"%s\" --export-secret-key %s", |
328 |
p, outfile, keyid); |
p, outfile, keyid); |
329 |
if (create_process (cmd, NULL, NULL)) |
if (create_process (cmd, NULL, NULL)) |
338 |
} |
} |
339 |
|
|
340 |
|
|
341 |
/* If EXPORTFLAG is 1, export the ownertrust data to the |
/* If @export is 1, export the ownertrust data to the |
342 |
buffer DATA. Otherwise import the ownertrust data from DATA. |
buffer @data. Otherwise import the ownertrust data from @data. |
343 |
Return value: 0 on success. */ |
Return value: 0 on success. */ |
344 |
gpgme_error_t |
gpgme_error_t |
345 |
gpg_manage_ownertrust (char **data, int exportflag) |
gpg_manage_ownertrust (char **data, int do_export) |
346 |
{ |
{ |
347 |
gpgme_error_t err = gpg_error (GPG_ERR_NO_ERROR); |
gpgme_error_t err = gpg_error (GPG_ERR_NO_ERROR); |
348 |
HANDLE out = NULL, in = NULL; |
HANDLE out = NULL, in = NULL; |
356 |
|
|
357 |
cmd = (char*)calloc (1, strlen (p) + 64 + 1); |
cmd = (char*)calloc (1, strlen (p) + 64 + 1); |
358 |
if (!cmd) |
if (!cmd) |
359 |
abort (); |
BUG (NULL); |
360 |
sprintf (cmd, "%s %s", p, |
sprintf (cmd, "%s %s", p, |
361 |
exportflag? "--export-ownertrust" : "--import-ownertrust"); |
do_export? "--export-ownertrust" : "--import-ownertrust"); |
362 |
|
|
363 |
if (exportflag) |
if (do_export) |
364 |
out = create_tmpfile ("gpg_ot_out"); |
out = create_tmpfile ("gpg_ot_out"); |
365 |
else { |
else { |
366 |
DWORD nw; |
DWORD nw; |
400 |
return gpg_error (GPG_ERR_INV_ARG); |
return gpg_error (GPG_ERR_INV_ARG); |
401 |
cmd = (char*)calloc (1, strlen (p) + 64); |
cmd = (char*)calloc (1, strlen (p) + 64); |
402 |
if (!cmd) |
if (!cmd) |
403 |
abort (); |
BUG (NULL); |
404 |
sprintf (cmd, "%s --logger-fd=1 --rebuild-keydb-caches", p); |
sprintf (cmd, "%s --logger-fd=1 --rebuild-keydb-caches", p); |
405 |
|
|
406 |
if (r_inf) |
if (r_inf) |
433 |
return gpg_error (GPG_ERR_INV_ARG); |
return gpg_error (GPG_ERR_INV_ARG); |
434 |
cmd = (char*)calloc (1, strlen (p) + 32); |
cmd = (char*)calloc (1, strlen (p) + 32); |
435 |
if (!cmd) |
if (!cmd) |
436 |
abort (); |
BUG (NULL); |
437 |
sprintf (cmd, "%s --version", p); |
sprintf (cmd, "%s --version", p); |
438 |
|
|
439 |
out = create_tmpfile ("gpg_out"); |
out = create_tmpfile ("gpg_out"); |
465 |
|
|
466 |
cmd = (char*)calloc (1, strlen (p) + strlen (fname) + 2+2 + 64); |
cmd = (char*)calloc (1, strlen (p) + strlen (fname) + 2+2 + 64); |
467 |
if (!cmd) |
if (!cmd) |
468 |
abort (); |
BUG (NULL); |
469 |
sprintf (cmd, "%s --fixed-list-mode --with-colons \"%s\"", p, fname); |
sprintf (cmd, "%s --fixed-list-mode --with-colons \"%s\"", p, fname); |
470 |
|
|
471 |
out = create_tmpfile ("gpg_keys"); |
out = create_tmpfile ("gpg_keys"); |
500 |
n += strlen (cmt) + 1; |
n += strlen (cmt) + 1; |
501 |
p = (char*)calloc (1, n+1); |
p = (char*)calloc (1, n+1); |
502 |
if (!p) |
if (!p) |
503 |
abort (); |
BUG (NULL); |
504 |
sprintf (p, fmt, code, cmt? cmt : "", cmt? "\n" : "", pass? pass : ""); |
sprintf (p, fmt, code, cmt? cmt : "", cmt? "\n" : "", pass? pass : ""); |
505 |
return p; |
return p; |
506 |
} |
} |
526 |
|
|
527 |
cmd = (char*)calloc (1, strlen (p) + strlen (keyid)+1 + 128); |
cmd = (char*)calloc (1, strlen (p) + strlen (keyid)+1 + 128); |
528 |
if (!cmd) |
if (!cmd) |
529 |
abort (); |
BUG (NULL); |
530 |
sprintf (cmd, "%s --pgp7 --command-fd=0 --status-fd=2 --gen-revoke %s", |
sprintf (cmd, "%s --pgp7 --command-fd=0 --status-fd=2 --gen-revoke %s", |
531 |
p, keyid); |
p, keyid); |
532 |
|
|
568 |
|
|
569 |
cmd = (char*)calloc (1, strlen (p) + strlen (keyid)+1 + 128); |
cmd = (char*)calloc (1, strlen (p) + strlen (keyid)+1 + 128); |
570 |
if (!cmd) |
if (!cmd) |
571 |
abort (); |
BUG (NULL); |
572 |
sprintf (cmd, "%s --with-colons --fixed-list-mode --list-keys \"%s\"", |
sprintf (cmd, "%s --with-colons --fixed-list-mode --list-keys \"%s\"", |
573 |
p, keyid); |
p, keyid); |
574 |
|
|