1 |
twoaday |
121 |
/* wptGPGUtil.cpp - GPG util functions |
2 |
twoaday |
133 |
* Copyright (C) 2005, 2006 Timo Schulz |
3 |
werner |
36 |
* Copyright (C) 2005 g10 Code GmbH |
4 |
|
|
* |
5 |
|
|
* This file is part of WinPT. |
6 |
|
|
* |
7 |
|
|
* WinPT is free software; you can redistribute it and/or modify |
8 |
|
|
* it under the terms of the GNU General Public License as published by |
9 |
|
|
* the Free Software Foundation; either version 2 of the License, or |
10 |
|
|
* (at your option) any later version. |
11 |
|
|
* |
12 |
|
|
* WinPT is distributed in the hope that it will be useful, |
13 |
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
14 |
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
15 |
|
|
* GNU General Public License for more details. |
16 |
|
|
* |
17 |
|
|
* You should have received a copy of the GNU General Public License |
18 |
|
|
* along with WinPT; if not, write to the Free Software Foundation, |
19 |
|
|
* Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA |
20 |
|
|
*/ |
21 |
twoaday |
121 |
|
22 |
werner |
36 |
#ifdef HAVE_CONFIG_H |
23 |
|
|
#include <config.h> |
24 |
|
|
#endif |
25 |
|
|
|
26 |
|
|
#include <windows.h> |
27 |
|
|
#include <sys/stat.h> |
28 |
twoaday |
179 |
#include <string.h> |
29 |
|
|
#include <errno.h> |
30 |
werner |
36 |
|
31 |
twoaday |
121 |
#include "gpgme.h" |
32 |
|
|
#include "wptTypes.h" |
33 |
twoaday |
73 |
#include "wptErrors.h" |
34 |
twoaday |
175 |
#include "wptW32API.h" |
35 |
twoaday |
179 |
#include "wptGPG.h" |
36 |
|
|
#include "openpgp.h" |
37 |
werner |
36 |
|
38 |
twoaday |
175 |
|
39 |
twoaday |
193 |
/* safe wrapper around calloc. */ |
40 |
|
|
static void* |
41 |
|
|
xcalloc (size_t n, size_t m) |
42 |
|
|
{ |
43 |
|
|
void *p = calloc (n, m); |
44 |
|
|
if (!p) |
45 |
|
|
BUG (0); |
46 |
|
|
return p; |
47 |
|
|
} |
48 |
|
|
|
49 |
|
|
|
50 |
werner |
36 |
#define NROFHEXDIGITS 2 |
51 |
|
|
/* Convert two hexadecimal digits from STR to the value they |
52 |
|
|
represent. Returns -1 if one of the characters is not a |
53 |
|
|
hexadecimal digit. */ |
54 |
|
|
static int |
55 |
|
|
hextobyte (const unsigned char *str) |
56 |
|
|
{ |
57 |
|
|
int val = 0; |
58 |
|
|
int i; |
59 |
|
|
|
60 |
|
|
for (i = 0; i < NROFHEXDIGITS; i++) { |
61 |
|
|
if (*str >= '0' && *str <= '9') |
62 |
|
|
val += *str - '0'; |
63 |
|
|
else if (*str >= 'A' && *str <= 'F') |
64 |
|
|
val += 10 + *str - 'A'; |
65 |
|
|
else if (*str >= 'a' && *str <= 'f') |
66 |
|
|
val += 10 + *str - 'a'; |
67 |
|
|
else |
68 |
|
|
return -1; |
69 |
|
|
if (i < NROFHEXDIGITS - 1) |
70 |
|
|
val *= 16; |
71 |
|
|
str++; |
72 |
|
|
} |
73 |
|
|
return val; |
74 |
|
|
} |
75 |
|
|
|
76 |
|
|
/* Decode the C formatted string @src and store the result in the |
77 |
|
|
buffer @destp which is @len bytes long. If @len is zero, then a |
78 |
|
|
large enough buffer is allocated with malloc and @destp is set to |
79 |
|
|
the result. Currently, @len is only used to specify if allocation |
80 |
|
|
is desired or not, the caller is expected to make sure that @destp |
81 |
|
|
is large enough if @len is not zero. */ |
82 |
|
|
gpgme_error_t |
83 |
|
|
gpg_decode_c_string (const char *src, char **destp, size_t len) |
84 |
|
|
{ |
85 |
|
|
char *dest; |
86 |
|
|
|
87 |
|
|
/* Set up the destination buffer. */ |
88 |
|
|
if (len) { |
89 |
|
|
if (len < strlen (src) + 1) |
90 |
|
|
return gpg_error (GPG_ERR_TOO_SHORT); |
91 |
|
|
dest = *destp; |
92 |
|
|
} |
93 |
|
|
else { |
94 |
|
|
/* The converted string will never be larger than the original string. */ |
95 |
twoaday |
193 |
dest = (char*)xcalloc (1,strlen (src) + 1); |
96 |
werner |
36 |
*destp = dest; |
97 |
|
|
} |
98 |
|
|
|
99 |
|
|
/* Convert the string. */ |
100 |
|
|
while (*src) { |
101 |
|
|
if (*src != '\\') { |
102 |
|
|
*(dest++) = *(src++); |
103 |
|
|
continue; |
104 |
|
|
} |
105 |
|
|
|
106 |
|
|
switch (src[1]) { |
107 |
|
|
#define DECODE_ONE(match,result) \ |
108 |
|
|
case match: \ |
109 |
|
|
src += 2; \ |
110 |
|
|
*(dest++) = result; \ |
111 |
|
|
break; |
112 |
|
|
|
113 |
|
|
DECODE_ONE ('\'', '\''); |
114 |
|
|
DECODE_ONE ('\"', '\"'); |
115 |
|
|
DECODE_ONE ('\?', '\?'); |
116 |
|
|
DECODE_ONE ('\\', '\\'); |
117 |
|
|
DECODE_ONE ('a', '\a'); |
118 |
|
|
DECODE_ONE ('b', '\b'); |
119 |
|
|
DECODE_ONE ('f', '\f'); |
120 |
|
|
DECODE_ONE ('n', '\n'); |
121 |
|
|
DECODE_ONE ('r', '\r'); |
122 |
|
|
DECODE_ONE ('t', '\t'); |
123 |
|
|
DECODE_ONE ('v', '\v'); |
124 |
|
|
|
125 |
|
|
case 'x': { |
126 |
|
|
int val = hextobyte ((unsigned char*)&src[2]); |
127 |
|
|
if (val == -1) { /* Should not happen. */ |
128 |
|
|
*(dest++) = *(src++); |
129 |
|
|
*(dest++) = *(src++); |
130 |
|
|
if (*src) |
131 |
|
|
*(dest++) = *(src++); |
132 |
|
|
if (*src) |
133 |
|
|
*(dest++) = *(src++); |
134 |
|
|
} |
135 |
|
|
else { |
136 |
|
|
if (!val) { |
137 |
|
|
/* A binary zero is not representable in a C string. */ |
138 |
|
|
*(dest++) = '\\'; |
139 |
|
|
*(dest++) = '0'; |
140 |
|
|
} |
141 |
|
|
else |
142 |
|
|
*((unsigned char *) dest++) = val; |
143 |
|
|
src += 4; |
144 |
|
|
} |
145 |
|
|
} |
146 |
|
|
|
147 |
|
|
default: /* Should not happen. */ |
148 |
|
|
{ |
149 |
|
|
*(dest++) = *(src++); |
150 |
|
|
*(dest++) = *(src++); |
151 |
|
|
} |
152 |
|
|
} |
153 |
|
|
} |
154 |
|
|
*(dest++) = 0; |
155 |
|
|
return 0; |
156 |
|
|
} |
157 |
|
|
|
158 |
twoaday |
193 |
|
159 |
|
|
|
160 |
werner |
36 |
/* Replace %foo% entries with its real values. |
161 |
|
|
Return value: expanded path or NULL on error. */ |
162 |
|
|
static char * |
163 |
|
|
expand_path (const char *path) |
164 |
|
|
{ |
165 |
|
|
DWORD len; |
166 |
|
|
char *p; |
167 |
|
|
|
168 |
|
|
len = ExpandEnvironmentStrings (path, NULL, 0); |
169 |
|
|
if (!len) |
170 |
|
|
return NULL; |
171 |
|
|
len += 1; |
172 |
twoaday |
193 |
p = (char*)xcalloc (1, len+1); |
173 |
werner |
36 |
len = ExpandEnvironmentStrings (path, p, len); |
174 |
|
|
if (!len) { |
175 |
twoaday |
200 |
safe_free (p); |
176 |
werner |
36 |
return NULL; |
177 |
|
|
} |
178 |
|
|
return p; |
179 |
|
|
} |
180 |
|
|
|
181 |
|
|
|
182 |
|
|
/* Read a string from the W32 registry. The directory is given |
183 |
|
|
in @dir and the name of the value in @name, */ |
184 |
|
|
static char * |
185 |
|
|
read_w32_registry (HKEY root_key, const char *dir, const char *name) |
186 |
|
|
{ |
187 |
|
|
HKEY key_handle; |
188 |
|
|
DWORD n1, nbytes; |
189 |
|
|
DWORD type; |
190 |
|
|
char *result = NULL; |
191 |
|
|
|
192 |
twoaday |
219 |
if (RegOpenKeyEx (root_key, dir, 0, KEY_READ, &key_handle)) { |
193 |
|
|
log_debug ("read_w32_registry (%s, %s) failed.\r\n", dir, name); |
194 |
werner |
36 |
return NULL; /* no need for a RegClose, so return direct */ |
195 |
twoaday |
219 |
} |
196 |
werner |
36 |
|
197 |
|
|
nbytes = 1; |
198 |
|
|
if (RegQueryValueEx (key_handle, name, 0, NULL, NULL, &nbytes)) |
199 |
|
|
goto leave; |
200 |
twoaday |
193 |
result = (char*)xcalloc (1, (n1=nbytes+1)); |
201 |
werner |
36 |
if (RegQueryValueEx (key_handle, name, 0, &type, (BYTE*)result, &n1)) { |
202 |
twoaday |
200 |
safe_free (result); |
203 |
werner |
36 |
result = NULL; |
204 |
|
|
goto leave; |
205 |
|
|
} |
206 |
|
|
if (type == REG_EXPAND_SZ && strchr (result, '%')) { |
207 |
|
|
char *p = expand_path (result); |
208 |
twoaday |
200 |
safe_free (result); |
209 |
werner |
36 |
result = p; |
210 |
|
|
} |
211 |
|
|
|
212 |
|
|
leave: |
213 |
|
|
RegCloseKey (key_handle); |
214 |
|
|
return result; |
215 |
|
|
} |
216 |
|
|
|
217 |
twoaday |
193 |
|
218 |
twoaday |
133 |
static char* |
219 |
|
|
read_gpg_program (void) |
220 |
|
|
{ |
221 |
|
|
return read_w32_registry (HKEY_CURRENT_USER, |
222 |
|
|
"Software\\GNU\\GnuPG", "gpgProgram"); |
223 |
|
|
} |
224 |
werner |
36 |
|
225 |
twoaday |
133 |
|
226 |
werner |
36 |
/* Create a temp file based on the name of @name. |
227 |
|
|
Return value: handle to the file in case of success. */ |
228 |
|
|
static HANDLE |
229 |
|
|
create_tmpfile (const char *name) |
230 |
|
|
{ |
231 |
|
|
HANDLE out; |
232 |
twoaday |
121 |
SECURITY_ATTRIBUTES sec_attr; |
233 |
twoaday |
219 |
char tmp[MAX_PATH+64]; |
234 |
werner |
36 |
|
235 |
twoaday |
121 |
memset (&sec_attr, 0, sizeof sec_attr); |
236 |
|
|
sec_attr.bInheritHandle = TRUE; |
237 |
|
|
sec_attr.lpSecurityDescriptor = NULL; |
238 |
|
|
sec_attr.nLength = sizeof sec_attr; |
239 |
werner |
36 |
|
240 |
twoaday |
175 |
get_temp_name (tmp, DIM (tmp)-1, name); |
241 |
werner |
36 |
out = CreateFile (tmp, GENERIC_READ|GENERIC_WRITE, |
242 |
twoaday |
121 |
FILE_SHARE_WRITE, &sec_attr, |
243 |
werner |
36 |
OPEN_ALWAYS, FILE_FLAG_DELETE_ON_CLOSE, NULL); |
244 |
twoaday |
121 |
if (out == INVALID_HANDLE_VALUE) |
245 |
|
|
log_debug ("create_tmpfile: CreateFile failed ec=%d\r\n", |
246 |
|
|
(int)GetLastError ()); |
247 |
werner |
36 |
return out; |
248 |
|
|
} |
249 |
|
|
|
250 |
|
|
|
251 |
|
|
/* Create a pipe with a readable remote end and |
252 |
|
|
write the data from @dat to the local end. |
253 |
|
|
Return value: read handle on success. */ |
254 |
|
|
static HANDLE |
255 |
|
|
create_in_pipe (const char *dat) |
256 |
|
|
{ |
257 |
|
|
HANDLE r, w; |
258 |
|
|
SECURITY_ATTRIBUTES sec_attr; |
259 |
|
|
DWORD n; |
260 |
|
|
|
261 |
|
|
memset (&sec_attr, 0, sizeof sec_attr); |
262 |
|
|
sec_attr.bInheritHandle = TRUE; |
263 |
|
|
sec_attr.nLength = sizeof sec_attr; |
264 |
|
|
|
265 |
twoaday |
121 |
if (!CreatePipe (&r, &w, &sec_attr, 4096)) { |
266 |
|
|
log_debug ("create_in_pipe: CreatePipeFailed ec=%d\r\n", |
267 |
|
|
(int)GetLastError ()); |
268 |
werner |
36 |
return NULL; |
269 |
twoaday |
121 |
} |
270 |
werner |
36 |
|
271 |
|
|
WriteFile (w, dat, strlen (dat), &n, NULL); |
272 |
|
|
CloseHandle (w); |
273 |
|
|
|
274 |
|
|
return r; |
275 |
|
|
} |
276 |
|
|
|
277 |
|
|
|
278 |
|
|
/* Map the contents of the file handle @out to |
279 |
|
|
a buffer and return it. */ |
280 |
|
|
static char* |
281 |
twoaday |
133 |
map_tmpfile (HANDLE out, DWORD *nread) |
282 |
werner |
36 |
{ |
283 |
|
|
DWORD n; |
284 |
|
|
char *p; |
285 |
|
|
|
286 |
|
|
FlushFileBuffers (out); |
287 |
|
|
SetFilePointer (out, 0, NULL, FILE_BEGIN); |
288 |
|
|
n = GetFileSize (out, NULL); |
289 |
twoaday |
193 |
p = (char*)xcalloc (1, n+1); |
290 |
werner |
36 |
ReadFile (out, p, n, &n, NULL); |
291 |
|
|
p[n] = 0; |
292 |
twoaday |
133 |
if (nread) |
293 |
|
|
*nread = n; |
294 |
werner |
36 |
return p; |
295 |
|
|
} |
296 |
|
|
|
297 |
|
|
|
298 |
|
|
/* Create a process from the command line in @cmd. |
299 |
|
|
If @out is != NULL, the output of the process will |
300 |
|
|
be redirected to @out. If @in is != NULL the input |
301 |
|
|
will be read from @in. |
302 |
|
|
Return value: 0 on success. */ |
303 |
|
|
static int |
304 |
twoaday |
133 |
create_process (const char *cmd, HANDLE in, HANDLE out, HANDLE err) |
305 |
werner |
36 |
{ |
306 |
|
|
STARTUPINFO si; |
307 |
|
|
PROCESS_INFORMATION pi; |
308 |
|
|
|
309 |
|
|
memset (&si, 0, sizeof (si)); |
310 |
|
|
si.cb = sizeof si; |
311 |
twoaday |
133 |
if (in || out || err) |
312 |
twoaday |
121 |
si.dwFlags = STARTF_USESTDHANDLES; |
313 |
werner |
36 |
if (out) |
314 |
|
|
si.hStdOutput = out; |
315 |
|
|
if (in) |
316 |
|
|
si.hStdInput = in; |
317 |
twoaday |
133 |
if (err) |
318 |
|
|
si.hStdError = err; |
319 |
twoaday |
121 |
si.dwFlags |= STARTF_USESHOWWINDOW; |
320 |
werner |
36 |
si.wShowWindow = SW_HIDE; |
321 |
|
|
if (!CreateProcess (NULL, (char*)cmd, NULL, NULL, TRUE, 0, |
322 |
twoaday |
121 |
NULL, NULL, &si, &pi)) { |
323 |
|
|
log_debug ("create_process: CreateProcess failed ec=%d\r\n", |
324 |
twoaday |
73 |
(int)GetLastError ()); |
325 |
werner |
36 |
return -1; |
326 |
twoaday |
73 |
} |
327 |
werner |
36 |
WaitForSingleObject (pi.hProcess, INFINITE); |
328 |
|
|
CloseHandle (pi.hProcess); |
329 |
twoaday |
270 |
CloseHandle (pi.hThread); |
330 |
werner |
36 |
return 0; |
331 |
|
|
} |
332 |
|
|
|
333 |
|
|
|
334 |
|
|
/* Export a GPG secret key given by @keyid into the file @outfile. |
335 |
|
|
Return value: 0 on success. */ |
336 |
|
|
gpgme_error_t |
337 |
|
|
gpg_export_seckey (const char *keyid, const char *outfile) |
338 |
|
|
{ |
339 |
|
|
gpgme_error_t err = gpg_error (GPG_ERR_NO_ERROR); |
340 |
|
|
struct stat st; |
341 |
twoaday |
200 |
const char *fmt; |
342 |
werner |
36 |
char *p; |
343 |
|
|
char *cmd; |
344 |
|
|
|
345 |
twoaday |
133 |
p = read_gpg_program (); |
346 |
werner |
36 |
if (!p) |
347 |
|
|
return gpg_error (GPG_ERR_INV_ARG); |
348 |
twoaday |
200 |
fmt = "%s --armor --yes --output \"%s\" --export-secret-key %s"; |
349 |
twoaday |
193 |
cmd = (char*)xcalloc (1, strlen (p) + strlen (keyid) |
350 |
twoaday |
200 |
+ strlen (outfile) + strlen (fmt) + 2); |
351 |
|
|
sprintf (cmd, fmt, p, outfile, keyid); |
352 |
twoaday |
133 |
if (create_process (cmd, NULL, NULL, NULL)) |
353 |
werner |
36 |
err = gpg_error (GPG_ERR_INTERNAL); |
354 |
|
|
|
355 |
|
|
if (stat (outfile, &st) == -1 || st.st_size == 0) |
356 |
|
|
err = gpg_error (GPG_ERR_NO_DATA); |
357 |
|
|
|
358 |
twoaday |
200 |
safe_free (p); |
359 |
|
|
safe_free (cmd); |
360 |
werner |
36 |
return err; |
361 |
|
|
} |
362 |
|
|
|
363 |
|
|
|
364 |
twoaday |
121 |
/* If @export is 1, export the ownertrust data to the |
365 |
|
|
buffer @data. Otherwise import the ownertrust data from @data. |
366 |
werner |
36 |
Return value: 0 on success. */ |
367 |
|
|
gpgme_error_t |
368 |
twoaday |
121 |
gpg_manage_ownertrust (char **data, int do_export) |
369 |
werner |
36 |
{ |
370 |
|
|
gpgme_error_t err = gpg_error (GPG_ERR_NO_ERROR); |
371 |
|
|
HANDLE out = NULL, in = NULL; |
372 |
|
|
char *p; |
373 |
|
|
char *cmd; |
374 |
|
|
|
375 |
twoaday |
133 |
p = read_gpg_program (); |
376 |
werner |
36 |
if (!p) |
377 |
|
|
return gpg_error (GPG_ERR_INV_ARG); |
378 |
|
|
|
379 |
twoaday |
193 |
cmd = (char*)xcalloc (1, strlen (p) + 64 + 1); |
380 |
twoaday |
219 |
sprintf (cmd, "%s %s", p, |
381 |
twoaday |
121 |
do_export? "--export-ownertrust" : "--import-ownertrust"); |
382 |
werner |
36 |
|
383 |
twoaday |
121 |
if (do_export) |
384 |
werner |
36 |
out = create_tmpfile ("gpg_ot_out"); |
385 |
|
|
else { |
386 |
|
|
DWORD nw; |
387 |
|
|
in = create_tmpfile ("gpg_ot_in"); |
388 |
|
|
WriteFile (in, *data, strlen (*data), &nw, NULL); |
389 |
|
|
FlushFileBuffers (in); |
390 |
|
|
} |
391 |
twoaday |
133 |
if (create_process (cmd, in, out, NULL)) |
392 |
werner |
36 |
err = gpg_error (GPG_ERR_INTERNAL); |
393 |
|
|
|
394 |
twoaday |
200 |
safe_free (p); |
395 |
|
|
safe_free (cmd); |
396 |
werner |
36 |
|
397 |
|
|
if (in) |
398 |
|
|
CloseHandle (in); |
399 |
|
|
if (out) { |
400 |
twoaday |
133 |
*data = map_tmpfile (out, NULL); |
401 |
werner |
36 |
CloseHandle (out); |
402 |
|
|
} |
403 |
twoaday |
129 |
|
404 |
werner |
36 |
return err; |
405 |
|
|
} |
406 |
|
|
|
407 |
|
|
|
408 |
|
|
/* Call gpg --rebuild-keydb-caches to speed up signature listings. */ |
409 |
|
|
gpgme_error_t |
410 |
|
|
gpg_rebuild_cache (char **r_inf) |
411 |
|
|
{ |
412 |
|
|
gpgme_error_t err = gpg_error (GPG_ERR_NO_ERROR); |
413 |
|
|
HANDLE out = NULL; |
414 |
twoaday |
193 |
const char *fmt; |
415 |
werner |
36 |
char *p; |
416 |
|
|
char *cmd; |
417 |
|
|
|
418 |
twoaday |
133 |
p = read_gpg_program (); |
419 |
werner |
36 |
if (!p) |
420 |
|
|
return gpg_error (GPG_ERR_INV_ARG); |
421 |
twoaday |
193 |
fmt = "%s --logger-fd=1 --batch --rebuild-keydb-caches"; |
422 |
|
|
cmd = (char*)xcalloc (1, strlen (p) + strlen (fmt) + 32); |
423 |
|
|
sprintf (cmd, fmt, p); |
424 |
werner |
36 |
|
425 |
|
|
if (r_inf) |
426 |
|
|
out = create_tmpfile ("gpg_rebuild_cache"); |
427 |
|
|
|
428 |
twoaday |
133 |
if (create_process (cmd, NULL, out, NULL)) |
429 |
werner |
36 |
err = gpg_error (GPG_ERR_INTERNAL); |
430 |
|
|
|
431 |
|
|
if (r_inf) |
432 |
twoaday |
133 |
*r_inf = map_tmpfile (out, NULL); |
433 |
werner |
36 |
if (out) |
434 |
|
|
CloseHandle (out); |
435 |
twoaday |
200 |
safe_free (p); |
436 |
|
|
safe_free (cmd); |
437 |
twoaday |
219 |
return err; |
438 |
werner |
36 |
} |
439 |
|
|
|
440 |
|
|
|
441 |
|
|
/* Call gpg --version to retrieve the 'about' information. */ |
442 |
|
|
gpgme_error_t |
443 |
|
|
gpg_get_version (char **r_inf) |
444 |
|
|
{ |
445 |
|
|
gpgme_error_t err= gpg_error (GPG_ERR_NO_ERROR); |
446 |
twoaday |
270 |
HANDLE out; |
447 |
|
|
const char *fmt; |
448 |
werner |
36 |
char *p, *cmd; |
449 |
|
|
|
450 |
twoaday |
133 |
p = read_gpg_program (); |
451 |
werner |
36 |
if (!p) |
452 |
|
|
return gpg_error (GPG_ERR_INV_ARG); |
453 |
twoaday |
270 |
fmt = "%s --version"; |
454 |
|
|
cmd = (char*)xcalloc (1, strlen (p) + strlen (fmt) + 1); |
455 |
|
|
sprintf (cmd, fmt, p); |
456 |
werner |
36 |
|
457 |
|
|
out = create_tmpfile ("gpg_out"); |
458 |
twoaday |
133 |
if (create_process (cmd, NULL, out, NULL)) |
459 |
werner |
36 |
err = gpg_error (GPG_ERR_INTERNAL); |
460 |
|
|
|
461 |
twoaday |
200 |
safe_free (p); |
462 |
|
|
safe_free (cmd); |
463 |
werner |
36 |
|
464 |
twoaday |
133 |
*r_inf = map_tmpfile (out, NULL); |
465 |
werner |
36 |
CloseHandle (out); |
466 |
|
|
return err; |
467 |
|
|
} |
468 |
|
|
|
469 |
|
|
|
470 |
|
|
/* Return the colon file output of the given file @fname in @r_out. |
471 |
|
|
Return value: 0 on success. */ |
472 |
|
|
gpgme_error_t |
473 |
|
|
gpg_import_key_list (const char *fname, char **r_out) |
474 |
|
|
{ |
475 |
|
|
gpgme_error_t err = gpg_error (GPG_ERR_NO_ERROR); |
476 |
twoaday |
200 |
const char *fmt; |
477 |
werner |
36 |
char *cmd, *p; |
478 |
|
|
HANDLE out; |
479 |
|
|
|
480 |
twoaday |
133 |
p = read_gpg_program (); |
481 |
werner |
36 |
if (!p) |
482 |
|
|
return gpg_error (GPG_ERR_INV_ARG); |
483 |
|
|
|
484 |
twoaday |
200 |
fmt = "%s --fixed-list-mode --with-colons \"%s\""; |
485 |
|
|
cmd = (char*)xcalloc (1, strlen (p) + strlen (fname) + strlen (fmt) + 2); |
486 |
|
|
sprintf (cmd, fmt, p, fname); |
487 |
werner |
36 |
|
488 |
|
|
out = create_tmpfile ("gpg_keys"); |
489 |
twoaday |
133 |
if (create_process (cmd, NULL, out, NULL)) |
490 |
werner |
36 |
err = gpg_error (GPG_ERR_INTERNAL); |
491 |
|
|
|
492 |
twoaday |
200 |
safe_free (p); |
493 |
|
|
safe_free (cmd); |
494 |
werner |
36 |
|
495 |
twoaday |
133 |
*r_out = map_tmpfile (out, NULL); |
496 |
werner |
36 |
CloseHandle (out); |
497 |
|
|
return err; |
498 |
|
|
} |
499 |
|
|
|
500 |
twoaday |
129 |
|
501 |
werner |
36 |
/* Generate a revocation certificate for the key with the keyid @keyid. |
502 |
|
|
@inp_data contains all needed data to answer the questions of the |
503 |
|
|
command handler. Each separate with a '\n'. |
504 |
|
|
@r_revcert contains the revocation cert on success. |
505 |
twoaday |
208 |
if @desig_revoke is 1, the designated revoker mode is used. |
506 |
werner |
36 |
Return value: 0 on success. */ |
507 |
|
|
gpgme_error_t |
508 |
twoaday |
208 |
gpg_revoke_cert (int desig_revoke, const char *inp_data, |
509 |
|
|
const char *keyid, char **r_revcert) |
510 |
werner |
36 |
{ |
511 |
|
|
gpgme_error_t err = gpg_error (GPG_ERR_NO_ERROR); |
512 |
twoaday |
208 |
const char *fmt, *revcmd; |
513 |
werner |
36 |
char *rcrt; |
514 |
|
|
char *cmd, *p; |
515 |
|
|
HANDLE in, out; |
516 |
|
|
|
517 |
twoaday |
133 |
p = read_gpg_program (); |
518 |
werner |
36 |
if (!p) |
519 |
|
|
return gpg_error (GPG_ERR_INV_ARG); |
520 |
|
|
|
521 |
twoaday |
208 |
revcmd = desig_revoke? "--desig-revoke" : "--gen-revoke"; |
522 |
|
|
fmt = "%s --pgp7 --command-fd=0 --status-fd=2 %s %s"; |
523 |
|
|
cmd = (char*)xcalloc (1, strlen (p) + strlen (revcmd) + |
524 |
|
|
strlen (keyid) + strlen (fmt) + 2); |
525 |
|
|
sprintf (cmd, fmt, p, revcmd, keyid); |
526 |
werner |
36 |
|
527 |
|
|
in = create_in_pipe (inp_data); |
528 |
|
|
out = create_tmpfile ("gpg_revcert"); |
529 |
twoaday |
133 |
if (create_process (cmd, in, out, NULL)) { |
530 |
werner |
36 |
*r_revcert = NULL; |
531 |
|
|
err = gpg_error (GPG_ERR_INTERNAL); |
532 |
|
|
} |
533 |
|
|
else { |
534 |
twoaday |
133 |
rcrt = map_tmpfile (out, NULL); |
535 |
werner |
36 |
*r_revcert = rcrt; |
536 |
twoaday |
129 |
if (rcrt && strlen (rcrt) == 0) |
537 |
|
|
err = gpg_error (GPG_ERR_BAD_PASSPHRASE); |
538 |
werner |
36 |
} |
539 |
|
|
|
540 |
twoaday |
200 |
safe_free (p); |
541 |
|
|
safe_free (cmd); |
542 |
werner |
36 |
|
543 |
|
|
CloseHandle (in); |
544 |
|
|
CloseHandle (out); |
545 |
|
|
return err; |
546 |
|
|
} |
547 |
|
|
|
548 |
|
|
|
549 |
twoaday |
133 |
/* Return the raw photo-id data combined with the status-fd |
550 |
|
|
entry in @r_data. If @keyid is set, only the data for this |
551 |
|
|
key will be returned. |
552 |
|
|
Return value: 0 on success. */ |
553 |
|
|
gpgme_error_t |
554 |
|
|
gpg_get_photoid_data (const char *keyid, char **r_status_data, |
555 |
|
|
unsigned char **r_data, unsigned long *ndata) |
556 |
|
|
{ |
557 |
|
|
gpgme_error_t err = gpg_error (GPG_ERR_NO_ERROR); |
558 |
|
|
HANDLE herr, hdat; |
559 |
twoaday |
200 |
const char *fmt; |
560 |
twoaday |
133 |
char *p, *cmd; |
561 |
|
|
DWORD n; |
562 |
|
|
|
563 |
twoaday |
164 |
if (ndata) |
564 |
|
|
*ndata = 0; |
565 |
twoaday |
133 |
p = read_gpg_program (); |
566 |
|
|
if (!p) |
567 |
|
|
return gpg_error (GPG_ERR_INV_ARG); |
568 |
twoaday |
200 |
fmt = "%s --attribute-fd=%d --status-fd=2 --list-keys %s"; |
569 |
|
|
n = strlen (p) + strlen (fmt) + 1; |
570 |
twoaday |
133 |
if (keyid) |
571 |
|
|
n += strlen (keyid) + 1; |
572 |
twoaday |
193 |
cmd = (char*)xcalloc (1, n+1); |
573 |
twoaday |
200 |
/* XXX: add --list-options show-unsuable-uid to display |
574 |
|
|
revoked attribute IDs */ |
575 |
twoaday |
133 |
hdat = create_tmpfile ("gpg_uat_data"); |
576 |
|
|
herr = create_tmpfile ("gpg_uat_status"); |
577 |
twoaday |
200 |
sprintf (cmd, fmt, p, (int)hdat, keyid? keyid : ""); |
578 |
twoaday |
133 |
if (create_process (cmd, NULL, NULL, herr)) |
579 |
|
|
err = gpg_error (GPG_ERR_INTERNAL); |
580 |
|
|
|
581 |
twoaday |
200 |
safe_free (p); |
582 |
|
|
safe_free (cmd); |
583 |
twoaday |
133 |
|
584 |
|
|
*r_data = (BYTE*)map_tmpfile (hdat, ndata); |
585 |
|
|
*r_status_data = map_tmpfile (herr, NULL); |
586 |
|
|
CloseHandle (hdat); |
587 |
|
|
CloseHandle (herr); |
588 |
|
|
|
589 |
|
|
return err; |
590 |
|
|
} |
591 |
|
|
|
592 |
|
|
|
593 |
twoaday |
175 |
/* Extract one or more keys from the key file @keyfile. |
594 |
|
|
The keys to extract are give in @keys and the size of it is @nkeys. |
595 |
|
|
@new_keyfile is a file with the extract keys. |
596 |
|
|
Return value: 0 on success. */ |
597 |
|
|
gpgme_error_t |
598 |
|
|
gpg_extract_keys (const char *keyfile, const char **keys, DWORD nkeys, |
599 |
|
|
char **new_keyfile) |
600 |
|
|
{ |
601 |
|
|
gpgme_error_t err = gpg_error (GPG_ERR_NO_ERROR); |
602 |
|
|
const char *fmt; |
603 |
|
|
char *p, *cmd; |
604 |
|
|
char tmpnam[MAX_PATH], tmpdir[MAX_PATH]; |
605 |
|
|
int i, n; |
606 |
|
|
|
607 |
|
|
/* copy key file to temp dir. */ |
608 |
|
|
GetTempPath (MAX_PATH-1, tmpdir); |
609 |
|
|
get_temp_name (tmpnam, MAX_PATH-1, NULL); |
610 |
|
|
CopyFile (keyfile, tmpnam, FALSE); |
611 |
|
|
|
612 |
|
|
/* create temp file for output. */ |
613 |
|
|
*new_keyfile = new char[MAX_PATH]; |
614 |
|
|
if (!*new_keyfile) |
615 |
|
|
BUG (NULL); |
616 |
|
|
get_temp_name (*new_keyfile, MAX_PATH-1, "sel_keys"); |
617 |
|
|
|
618 |
|
|
p = read_gpg_program (); |
619 |
|
|
if (!p) |
620 |
|
|
return gpg_error (GPG_ERR_INV_ARG); |
621 |
|
|
|
622 |
|
|
/* Use the temp key file as a keyring and export the selected |
623 |
|
|
keys from it. */ |
624 |
|
|
fmt = "%s --yes --output %s --no-options --homedir %s --keyring %s --export "; |
625 |
|
|
n = strlen (fmt) + strlen (p)+1 + strlen (tmpdir)+1 + strlen (tmpnam) + 1; |
626 |
|
|
n += strlen (*new_keyfile)+1; |
627 |
|
|
for (i=0; i < (int)nkeys; i++) |
628 |
|
|
n += strlen (keys[i])+1+2; |
629 |
twoaday |
193 |
cmd = (char*)xcalloc (1, n+1); |
630 |
twoaday |
175 |
sprintf (cmd, fmt, p, *new_keyfile, tmpdir, tmpnam); |
631 |
|
|
for (i=0; i < (int)nkeys; i++) { |
632 |
|
|
strcat (cmd, keys[i]); |
633 |
|
|
strcat (cmd, " " ); |
634 |
|
|
} |
635 |
|
|
|
636 |
|
|
if (create_process (cmd, NULL, NULL, NULL)) |
637 |
|
|
err = gpgme_error (GPG_ERR_INTERNAL); |
638 |
|
|
|
639 |
|
|
DeleteFile (tmpnam); |
640 |
|
|
safe_free (cmd); |
641 |
|
|
safe_free (p); |
642 |
|
|
return err; |
643 |
|
|
} |
644 |
|
|
|
645 |
|
|
|
646 |
werner |
36 |
/* Return the validity of the user attribute, informerly known |
647 |
|
|
as photo-ID. If no uat was found, return 0 for unknown. */ |
648 |
|
|
gpgme_error_t |
649 |
|
|
get_uat_validity (const char *keyid, gpgme_validity_t *r_valid) |
650 |
|
|
{ |
651 |
|
|
gpgme_error_t err = gpg_error (GPG_ERR_NO_ERROR); |
652 |
|
|
HANDLE out; |
653 |
twoaday |
200 |
const char *fmt; |
654 |
werner |
36 |
char *p, *cmd; |
655 |
|
|
char *uat; |
656 |
|
|
|
657 |
|
|
*r_valid = GPGME_VALIDITY_UNKNOWN; |
658 |
twoaday |
133 |
p = read_gpg_program (); |
659 |
werner |
36 |
if (!p) |
660 |
|
|
return gpg_error (GPG_ERR_INV_ARG); |
661 |
|
|
|
662 |
twoaday |
200 |
fmt = "%s --with-colons --fixed-list-mode --list-keys \"%s\""; |
663 |
|
|
cmd = (char*)xcalloc (1, strlen (p) + strlen (keyid) + strlen (fmt) + 2); |
664 |
|
|
sprintf (cmd, fmt, p, keyid); |
665 |
werner |
36 |
|
666 |
|
|
out = create_tmpfile ("gpg_keys"); |
667 |
twoaday |
133 |
if (create_process (cmd, NULL, out, NULL)) |
668 |
werner |
36 |
err = gpg_error (GPG_ERR_INTERNAL); |
669 |
|
|
|
670 |
twoaday |
175 |
safe_free (p); |
671 |
|
|
safe_free (cmd); |
672 |
werner |
36 |
|
673 |
twoaday |
133 |
p = map_tmpfile (out, NULL); |
674 |
werner |
36 |
if ((uat = strstr (p, "uat:"))) { |
675 |
|
|
switch (*(uat+4)) { |
676 |
|
|
case 'm': *r_valid = GPGME_VALIDITY_MARGINAL; break; |
677 |
|
|
case 'f': |
678 |
|
|
case 'u': *r_valid = GPGME_VALIDITY_FULL; break; |
679 |
twoaday |
219 |
default : *r_valid = GPGME_VALIDITY_UNDEFINED; break; |
680 |
werner |
36 |
} |
681 |
|
|
} |
682 |
|
|
|
683 |
twoaday |
200 |
safe_free (p); |
684 |
werner |
36 |
CloseHandle (out); |
685 |
|
|
return err; |
686 |
|
|
} |
687 |
|
|
|
688 |
|
|
|
689 |
twoaday |
179 |
static gpgme_error_t |
690 |
|
|
clip_store_data (char *tmp_outname, DWORD outlen) |
691 |
|
|
{ |
692 |
|
|
gpgme_data_t in; |
693 |
|
|
gpgme_error_t err; |
694 |
|
|
|
695 |
|
|
get_temp_name (tmp_outname, outlen-1, NULL); |
696 |
|
|
err = gpg_data_new_from_clipboard (&in, 0); |
697 |
|
|
if (err) |
698 |
|
|
return err; |
699 |
|
|
err = gpg_data_release_and_set_file (in, tmp_outname); |
700 |
|
|
return err; |
701 |
|
|
} |
702 |
|
|
|
703 |
|
|
|
704 |
werner |
36 |
/* Extract all recipients from the file @file. |
705 |
|
|
Return value: 0 on success. */ |
706 |
twoaday |
179 |
static gpgme_error_t |
707 |
|
|
file_extract_recipient (const char *file, gpgme_recipient_t *r_list) |
708 |
|
|
{ |
709 |
werner |
36 |
PACKET *pkt; |
710 |
twoaday |
179 |
PKT_pubkey_enc *enc; |
711 |
werner |
36 |
gpg_iobuf_t inp = NULL; |
712 |
|
|
armor_filter_context_t afx; |
713 |
twoaday |
179 |
gpgme_recipient_t l; |
714 |
werner |
36 |
int rc = 0, quit=0; |
715 |
|
|
|
716 |
|
|
if (!file || !r_list) { |
717 |
|
|
log_debug ("do_list_packets: !r_list || !file"); |
718 |
twoaday |
179 |
return gpgme_error (GPG_ERR_INV_ARG); |
719 |
werner |
36 |
} |
720 |
|
|
|
721 |
twoaday |
200 |
*r_list = NULL; |
722 |
werner |
36 |
inp = gpg_iobuf_open (file); |
723 |
|
|
if (!inp) |
724 |
twoaday |
179 |
return gpgme_err_code_from_errno (errno); |
725 |
werner |
36 |
gpg_iobuf_ioctl (inp, 3, 1, NULL); /* disable cache */ |
726 |
|
|
if (gpg_use_armor_filter (inp)) { |
727 |
|
|
memset (&afx, 0, sizeof (afx)); |
728 |
|
|
gpg_iobuf_push_filter (inp, gpg_armor_filter, &afx); |
729 |
|
|
} |
730 |
twoaday |
193 |
pkt = (PACKET *)xcalloc(1, sizeof *pkt); |
731 |
werner |
36 |
gpg_init_packet (pkt); |
732 |
|
|
while (!quit && (rc = gpg_parse_packet (inp, pkt)) != -1) { |
733 |
|
|
switch (pkt->pkttype) { |
734 |
|
|
case PKT_PUBKEY_ENC: |
735 |
twoaday |
179 |
enc = pkt->pkt.pubkey_enc; |
736 |
werner |
36 |
if (!enc) |
737 |
|
|
break; |
738 |
twoaday |
193 |
l = (gpgme_recipient_t)xcalloc (1, sizeof *l); |
739 |
|
|
l->keyid = (char*)xcalloc (1, 16+1); |
740 |
twoaday |
219 |
_snprintf (l->keyid, 16, "%08lX%08lX", |
741 |
|
|
enc->keyid[0], enc->keyid[1]); |
742 |
twoaday |
179 |
l->pubkey_algo = (gpgme_pubkey_algo_t)enc->pubkey_algo; |
743 |
werner |
36 |
l->status = 0; |
744 |
|
|
l->next = (*r_list); |
745 |
|
|
*r_list = l; |
746 |
twoaday |
179 |
break; |
747 |
werner |
36 |
|
748 |
|
|
case PKT_ENCRYPTED: |
749 |
|
|
case PKT_ENCRYPTED_MDC: |
750 |
twoaday |
200 |
case PKT_PLAINTEXT: |
751 |
werner |
36 |
case PKT_COMPRESSED: |
752 |
|
|
case PKT_PUBLIC_KEY: |
753 |
|
|
case PKT_SECRET_KEY: |
754 |
|
|
quit = 1; |
755 |
|
|
break; |
756 |
twoaday |
181 |
|
757 |
twoaday |
180 |
default: |
758 |
|
|
break; |
759 |
werner |
36 |
} |
760 |
|
|
gpg_free_packet (pkt); |
761 |
|
|
gpg_init_packet (pkt); |
762 |
|
|
} |
763 |
|
|
gpg_iobuf_close (inp); |
764 |
|
|
safe_free (pkt); |
765 |
|
|
return 0; |
766 |
|
|
} |
767 |
twoaday |
179 |
|
768 |
|
|
|
769 |
|
|
/* Either extract the list of recipients from the file @file or |
770 |
|
|
if the string is NULL, try to extract them from the clipboard. |
771 |
|
|
Return value: 0 on success. */ |
772 |
|
|
gpgme_error_t |
773 |
|
|
gpg_get_recipients (const char *file, gpgme_recipient_t *r_list) |
774 |
|
|
{ |
775 |
|
|
gpgme_error_t err; |
776 |
|
|
char tmp[MAX_PATH+1]; |
777 |
|
|
|
778 |
|
|
if (!file) { |
779 |
|
|
clip_store_data (tmp, DIM (tmp)-2); |
780 |
|
|
err = file_extract_recipient (tmp, r_list); |
781 |
|
|
DeleteFile (tmp); |
782 |
|
|
} |
783 |
|
|
else |
784 |
|
|
err = file_extract_recipient (file, r_list); |
785 |
|
|
return err; |
786 |
|
|
} |
787 |
twoaday |
181 |
|
788 |
|
|
|
789 |
|
|
/* Try to find a subpacket with the given id @subpktid |
790 |
|
|
inside the key @key. |
791 |
|
|
Return value: 0 on success. */ |
792 |
|
|
gpgme_error_t |
793 |
twoaday |
219 |
gpg_find_key_subpacket (const char *key, int subpktid, char **value) |
794 |
twoaday |
181 |
{ |
795 |
|
|
gpgme_error_t err = gpg_error (GPG_ERR_NO_ERROR); |
796 |
|
|
const char *fmt; |
797 |
|
|
const char *spk; |
798 |
|
|
char *p, *cmd; |
799 |
|
|
HANDLE out; |
800 |
|
|
|
801 |
|
|
*value = NULL; |
802 |
|
|
p = read_gpg_program (); |
803 |
twoaday |
214 |
fmt = "%s --with-colons --no-options --list-options show-sig-subpackets=%d --list-sigs %s"; |
804 |
twoaday |
193 |
cmd = (char*)xcalloc (1, strlen (fmt) + strlen (p) + strlen (key) + 32 + 1); |
805 |
twoaday |
181 |
sprintf (cmd, fmt, p, subpktid, key); |
806 |
|
|
|
807 |
|
|
out = create_tmpfile ("gpg_subpackets"); |
808 |
|
|
if (create_process (cmd, NULL, out, NULL)) |
809 |
|
|
err = gpg_error (GPG_ERR_INTERNAL); |
810 |
|
|
|
811 |
twoaday |
200 |
safe_free (p); |
812 |
|
|
safe_free (cmd); |
813 |
twoaday |
181 |
|
814 |
twoaday |
219 |
p = map_tmpfile (out, NULL); |
815 |
twoaday |
181 |
if (p && (spk=strstr (p, "spk"))) { |
816 |
|
|
char *end = strstr (spk, "\n"); |
817 |
twoaday |
200 |
|
818 |
twoaday |
181 |
if (end) { |
819 |
twoaday |
193 |
*value = (char*)xcalloc (1, (end-spk)+1); |
820 |
twoaday |
181 |
memcpy (*value, spk, (end-spk)-1); |
821 |
|
|
} |
822 |
|
|
} |
823 |
|
|
|
824 |
twoaday |
200 |
safe_free (p); |
825 |
twoaday |
181 |
CloseHandle (out); |
826 |
|
|
return err; |
827 |
|
|
} |