/[winpt]/trunk/Src/wptGPGUtil.cpp
ViewVC logotype

Contents of /trunk/Src/wptGPGUtil.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 214 - (show annotations)
Sun May 14 18:40:36 2006 UTC (18 years, 9 months ago) by twoaday
File size: 20862 byte(s)
2006-05-14  Timo Schulz  <ts@g10code.de>
                                                                                
        * wptKeyCache.cpp (gpg_keycache_update_attr): Parse
        preferred keyserver URL.
        * wptHTTP.cpp (extractHostInfo): Fix segv.
        * wptGPGUtil.cpp (gpg_find_key_subpacket): Ignore default
        gpg.conf.
        * wptKeyserverSearchDlg.cpp (search_hkp_keys): Do not
        assume an existing user id.
        * wptPassphraseCB.cpp (passphrase_cb): Automatic cancel
        if no passphrase is available.

(for complete list of changes, see Src/ChangeLog)

About to release 0.12.1


1 /* wptGPGUtil.cpp - GPG util functions
2 * Copyright (C) 2005, 2006 Timo Schulz
3 * 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
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <windows.h>
27 #include <sys/stat.h>
28 #include <string.h>
29 #include <errno.h>
30
31 #include "gpgme.h"
32 #include "wptTypes.h"
33 #include "wptErrors.h"
34 #include "wptW32API.h"
35 #include "wptGPG.h"
36 #include "openpgp.h"
37
38
39 /* 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 #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 dest = (char*)xcalloc (1,strlen (src) + 1);
96 *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
159
160 /* 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 p = (char*)xcalloc (1, len+1);
173 len = ExpandEnvironmentStrings (path, p, len);
174 if (!len) {
175 safe_free (p);
176 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 if (RegOpenKeyEx (root_key, dir, 0, KEY_READ, &key_handle))
193 return NULL; /* no need for a RegClose, so return direct */
194
195 nbytes = 1;
196 if (RegQueryValueEx (key_handle, name, 0, NULL, NULL, &nbytes))
197 goto leave;
198 result = (char*)xcalloc (1, (n1=nbytes+1));
199 if (RegQueryValueEx (key_handle, name, 0, &type, (BYTE*)result, &n1)) {
200 safe_free (result);
201 result = NULL;
202 goto leave;
203 }
204 if (type == REG_EXPAND_SZ && strchr (result, '%')) {
205 char *p = expand_path (result);
206 safe_free (result);
207 result = p;
208 }
209
210 leave:
211 RegCloseKey (key_handle);
212 return result;
213 }
214
215
216 static char*
217 read_gpg_program (void)
218 {
219 return read_w32_registry (HKEY_CURRENT_USER,
220 "Software\\GNU\\GnuPG", "gpgProgram");
221 }
222
223
224 /* Create a temp file based on the name of @name.
225 Return value: handle to the file in case of success. */
226 static HANDLE
227 create_tmpfile (const char *name)
228 {
229 HANDLE out;
230 SECURITY_ATTRIBUTES sec_attr;
231 char tmp[300];
232
233 memset (&sec_attr, 0, sizeof sec_attr);
234 sec_attr.bInheritHandle = TRUE;
235 sec_attr.lpSecurityDescriptor = NULL;
236 sec_attr.nLength = sizeof sec_attr;
237
238 get_temp_name (tmp, DIM (tmp)-1, name);
239 out = CreateFile (tmp, GENERIC_READ|GENERIC_WRITE,
240 FILE_SHARE_WRITE, &sec_attr,
241 OPEN_ALWAYS, FILE_FLAG_DELETE_ON_CLOSE, NULL);
242 if (out == INVALID_HANDLE_VALUE)
243 log_debug ("create_tmpfile: CreateFile failed ec=%d\r\n",
244 (int)GetLastError ());
245 return out;
246 }
247
248
249 /* Create a pipe with a readable remote end and
250 write the data from @dat to the local end.
251 Return value: read handle on success. */
252 static HANDLE
253 create_in_pipe (const char *dat)
254 {
255 HANDLE r, w;
256 SECURITY_ATTRIBUTES sec_attr;
257 DWORD n;
258
259 memset (&sec_attr, 0, sizeof sec_attr);
260 sec_attr.bInheritHandle = TRUE;
261 sec_attr.nLength = sizeof sec_attr;
262
263 if (!CreatePipe (&r, &w, &sec_attr, 4096)) {
264 log_debug ("create_in_pipe: CreatePipeFailed ec=%d\r\n",
265 (int)GetLastError ());
266 return NULL;
267 }
268
269 WriteFile (w, dat, strlen (dat), &n, NULL);
270 CloseHandle (w);
271
272 return r;
273 }
274
275
276 /* Map the contents of the file handle @out to
277 a buffer and return it. */
278 static char*
279 map_tmpfile (HANDLE out, DWORD *nread)
280 {
281 DWORD n;
282 char *p;
283
284 FlushFileBuffers (out);
285 SetFilePointer (out, 0, NULL, FILE_BEGIN);
286 n = GetFileSize (out, NULL);
287 p = (char*)xcalloc (1, n+1);
288 ReadFile (out, p, n, &n, NULL);
289 p[n] = 0;
290 if (nread)
291 *nread = n;
292 return p;
293 }
294
295
296 /* Create a process from the command line in @cmd.
297 If @out is != NULL, the output of the process will
298 be redirected to @out. If @in is != NULL the input
299 will be read from @in.
300 Return value: 0 on success. */
301 static int
302 create_process (const char *cmd, HANDLE in, HANDLE out, HANDLE err)
303 {
304 STARTUPINFO si;
305 PROCESS_INFORMATION pi;
306
307 memset (&si, 0, sizeof (si));
308 si.cb = sizeof si;
309 if (in || out || err)
310 si.dwFlags = STARTF_USESTDHANDLES;
311 if (out)
312 si.hStdOutput = out;
313 if (in)
314 si.hStdInput = in;
315 if (err)
316 si.hStdError = err;
317 si.dwFlags |= STARTF_USESHOWWINDOW;
318 si.wShowWindow = SW_HIDE;
319 if (!CreateProcess (NULL, (char*)cmd, NULL, NULL, TRUE, 0,
320 NULL, NULL, &si, &pi)) {
321 log_debug ("create_process: CreateProcess failed ec=%d\r\n",
322 (int)GetLastError ());
323 return -1;
324 }
325 WaitForSingleObject (pi.hProcess, INFINITE);
326 CloseHandle (pi.hProcess);
327 return 0;
328 }
329
330
331 /* Export a GPG secret key given by @keyid into the file @outfile.
332 Return value: 0 on success. */
333 gpgme_error_t
334 gpg_export_seckey (const char *keyid, const char *outfile)
335 {
336 gpgme_error_t err = gpg_error (GPG_ERR_NO_ERROR);
337 struct stat st;
338 const char *fmt;
339 char *p;
340 char *cmd;
341
342 p = read_gpg_program ();
343 if (!p)
344 return gpg_error (GPG_ERR_INV_ARG);
345 fmt = "%s --armor --yes --output \"%s\" --export-secret-key %s";
346 cmd = (char*)xcalloc (1, strlen (p) + strlen (keyid)
347 + strlen (outfile) + strlen (fmt) + 2);
348 sprintf (cmd, fmt, p, outfile, keyid);
349 if (create_process (cmd, NULL, NULL, NULL))
350 err = gpg_error (GPG_ERR_INTERNAL);
351
352 if (stat (outfile, &st) == -1 || st.st_size == 0)
353 err = gpg_error (GPG_ERR_NO_DATA);
354
355 safe_free (p);
356 safe_free (cmd);
357 return err;
358 }
359
360
361 /* If @export is 1, export the ownertrust data to the
362 buffer @data. Otherwise import the ownertrust data from @data.
363 Return value: 0 on success. */
364 gpgme_error_t
365 gpg_manage_ownertrust (char **data, int do_export)
366 {
367 gpgme_error_t err = gpg_error (GPG_ERR_NO_ERROR);
368 HANDLE out = NULL, in = NULL;
369 char *p;
370 char *cmd;
371
372 p = read_gpg_program ();
373 if (!p)
374 return gpg_error (GPG_ERR_INV_ARG);
375
376 cmd = (char*)xcalloc (1, strlen (p) + 64 + 1);
377 sprintf (cmd, "%s %s", p,
378 do_export? "--export-ownertrust" : "--import-ownertrust");
379
380 if (do_export)
381 out = create_tmpfile ("gpg_ot_out");
382 else {
383 DWORD nw;
384 in = create_tmpfile ("gpg_ot_in");
385 WriteFile (in, *data, strlen (*data), &nw, NULL);
386 FlushFileBuffers (in);
387 /* XXX: need a rewind? */
388 }
389 if (create_process (cmd, in, out, NULL))
390 err = gpg_error (GPG_ERR_INTERNAL);
391
392 safe_free (p);
393 safe_free (cmd);
394
395 if (in)
396 CloseHandle (in);
397 if (out) {
398 *data = map_tmpfile (out, NULL);
399 CloseHandle (out);
400 }
401
402 return err;
403 }
404
405
406 /* Call gpg --rebuild-keydb-caches to speed up signature listings. */
407 gpgme_error_t
408 gpg_rebuild_cache (char **r_inf)
409 {
410 gpgme_error_t err = gpg_error (GPG_ERR_NO_ERROR);
411 HANDLE out = NULL;
412 const char *fmt;
413 char *p;
414 char *cmd;
415
416 p = read_gpg_program ();
417 if (!p)
418 return gpg_error (GPG_ERR_INV_ARG);
419 fmt = "%s --logger-fd=1 --batch --rebuild-keydb-caches";
420 cmd = (char*)xcalloc (1, strlen (p) + strlen (fmt) + 32);
421 sprintf (cmd, fmt, p);
422
423 if (r_inf)
424 out = create_tmpfile ("gpg_rebuild_cache");
425
426 if (create_process (cmd, NULL, out, NULL))
427 err = gpg_error (GPG_ERR_INTERNAL);
428
429 if (r_inf)
430 *r_inf = map_tmpfile (out, NULL);
431 if (out)
432 CloseHandle (out);
433 safe_free (p);
434 safe_free (cmd);
435 return 0;
436 }
437
438
439 /* Call gpg --version to retrieve the 'about' information. */
440 gpgme_error_t
441 gpg_get_version (char **r_inf)
442 {
443 gpgme_error_t err= gpg_error (GPG_ERR_NO_ERROR);
444 HANDLE out;
445 char *p, *cmd;
446
447 p = read_gpg_program ();
448 if (!p)
449 return gpg_error (GPG_ERR_INV_ARG);
450 cmd = (char*)xcalloc (1, strlen (p) + 32);
451 sprintf (cmd, "%s --version", p);
452
453 out = create_tmpfile ("gpg_out");
454 if (create_process (cmd, NULL, out, NULL))
455 err = gpg_error (GPG_ERR_INTERNAL);
456
457 safe_free (p);
458 safe_free (cmd);
459
460 *r_inf = map_tmpfile (out, NULL);
461 CloseHandle (out);
462 return err;
463 }
464
465
466 /* Return the colon file output of the given file @fname in @r_out.
467 Return value: 0 on success. */
468 gpgme_error_t
469 gpg_import_key_list (const char *fname, char **r_out)
470 {
471 gpgme_error_t err = gpg_error (GPG_ERR_NO_ERROR);
472 const char *fmt;
473 char *cmd, *p;
474 HANDLE out;
475
476 p = read_gpg_program ();
477 if (!p)
478 return gpg_error (GPG_ERR_INV_ARG);
479
480 fmt = "%s --fixed-list-mode --with-colons \"%s\"";
481 cmd = (char*)xcalloc (1, strlen (p) + strlen (fname) + strlen (fmt) + 2);
482 sprintf (cmd, fmt, p, fname);
483
484 out = create_tmpfile ("gpg_keys");
485 if (create_process (cmd, NULL, out, NULL))
486 err = gpg_error (GPG_ERR_INTERNAL);
487
488 safe_free (p);
489 safe_free (cmd);
490
491 *r_out = map_tmpfile (out, NULL);
492 CloseHandle (out);
493 return err;
494 }
495
496
497 /* Generate a revocation certificate for the key with the keyid @keyid.
498 @inp_data contains all needed data to answer the questions of the
499 command handler. Each separate with a '\n'.
500 @r_revcert contains the revocation cert on success.
501 if @desig_revoke is 1, the designated revoker mode is used.
502 Return value: 0 on success. */
503 gpgme_error_t
504 gpg_revoke_cert (int desig_revoke, const char *inp_data,
505 const char *keyid, char **r_revcert)
506 {
507 gpgme_error_t err = gpg_error (GPG_ERR_NO_ERROR);
508 const char *fmt, *revcmd;
509 char *rcrt;
510 char *cmd, *p;
511 HANDLE in, out;
512
513 p = read_gpg_program ();
514 if (!p)
515 return gpg_error (GPG_ERR_INV_ARG);
516
517 revcmd = desig_revoke? "--desig-revoke" : "--gen-revoke";
518 fmt = "%s --pgp7 --command-fd=0 --status-fd=2 %s %s";
519 cmd = (char*)xcalloc (1, strlen (p) + strlen (revcmd) +
520 strlen (keyid) + strlen (fmt) + 2);
521 sprintf (cmd, fmt, p, revcmd, keyid);
522
523 in = create_in_pipe (inp_data);
524 out = create_tmpfile ("gpg_revcert");
525 if (create_process (cmd, in, out, NULL)) {
526 *r_revcert = NULL;
527 err = gpg_error (GPG_ERR_INTERNAL);
528 }
529 else {
530 rcrt = map_tmpfile (out, NULL);
531 *r_revcert = rcrt;
532 if (rcrt && strlen (rcrt) == 0)
533 err = gpg_error (GPG_ERR_BAD_PASSPHRASE);
534 }
535
536 safe_free (p);
537 safe_free (cmd);
538
539 CloseHandle (in);
540 CloseHandle (out);
541 return err;
542 }
543
544
545 /* Return the raw photo-id data combined with the status-fd
546 entry in @r_data. If @keyid is set, only the data for this
547 key will be returned.
548 Return value: 0 on success. */
549 gpgme_error_t
550 gpg_get_photoid_data (const char *keyid, char **r_status_data,
551 unsigned char **r_data, unsigned long *ndata)
552 {
553 gpgme_error_t err = gpg_error (GPG_ERR_NO_ERROR);
554 HANDLE herr, hdat;
555 const char *fmt;
556 char *p, *cmd;
557 DWORD n;
558
559 if (ndata)
560 *ndata = 0;
561 p = read_gpg_program ();
562 if (!p)
563 return gpg_error (GPG_ERR_INV_ARG);
564 fmt = "%s --attribute-fd=%d --status-fd=2 --list-keys %s";
565 n = strlen (p) + strlen (fmt) + 1;
566 if (keyid)
567 n += strlen (keyid) + 1;
568 cmd = (char*)xcalloc (1, n+1);
569 /* XXX: add --list-options show-unsuable-uid to display
570 revoked attribute IDs */
571 hdat = create_tmpfile ("gpg_uat_data");
572 herr = create_tmpfile ("gpg_uat_status");
573 sprintf (cmd, fmt, p, (int)hdat, keyid? keyid : "");
574 if (create_process (cmd, NULL, NULL, herr))
575 err = gpg_error (GPG_ERR_INTERNAL);
576
577 safe_free (p);
578 safe_free (cmd);
579
580 *r_data = (BYTE*)map_tmpfile (hdat, ndata);
581 *r_status_data = map_tmpfile (herr, NULL);
582 CloseHandle (hdat);
583 CloseHandle (herr);
584
585 return err;
586 }
587
588
589 /* Extract one or more keys from the key file @keyfile.
590 The keys to extract are give in @keys and the size of it is @nkeys.
591 @new_keyfile is a file with the extract keys.
592 Return value: 0 on success. */
593 gpgme_error_t
594 gpg_extract_keys (const char *keyfile, const char **keys, DWORD nkeys,
595 char **new_keyfile)
596 {
597 gpgme_error_t err = gpg_error (GPG_ERR_NO_ERROR);
598 const char *fmt;
599 char *p, *cmd;
600 char tmpnam[MAX_PATH], tmpdir[MAX_PATH];
601 int i, n;
602
603 /* copy key file to temp dir. */
604 GetTempPath (MAX_PATH-1, tmpdir);
605 get_temp_name (tmpnam, MAX_PATH-1, NULL);
606 CopyFile (keyfile, tmpnam, FALSE);
607
608 /* create temp file for output. */
609 *new_keyfile = new char[MAX_PATH];
610 if (!*new_keyfile)
611 BUG (NULL);
612 get_temp_name (*new_keyfile, MAX_PATH-1, "sel_keys");
613
614 p = read_gpg_program ();
615 if (!p)
616 return gpg_error (GPG_ERR_INV_ARG);
617
618 /* Use the temp key file as a keyring and export the selected
619 keys from it. */
620 fmt = "%s --yes --output %s --no-options --homedir %s --keyring %s --export ";
621 n = strlen (fmt) + strlen (p)+1 + strlen (tmpdir)+1 + strlen (tmpnam) + 1;
622 n += strlen (*new_keyfile)+1;
623 for (i=0; i < (int)nkeys; i++)
624 n += strlen (keys[i])+1+2;
625 cmd = (char*)xcalloc (1, n+1);
626 sprintf (cmd, fmt, p, *new_keyfile, tmpdir, tmpnam);
627 for (i=0; i < (int)nkeys; i++) {
628 strcat (cmd, keys[i]);
629 strcat (cmd, " " );
630 }
631
632 if (create_process (cmd, NULL, NULL, NULL))
633 err = gpgme_error (GPG_ERR_INTERNAL);
634
635 DeleteFile (tmpnam);
636 safe_free (cmd);
637 safe_free (p);
638 return err;
639 }
640
641
642 /* Return the validity of the user attribute, informerly known
643 as photo-ID. If no uat was found, return 0 for unknown. */
644 gpgme_error_t
645 get_uat_validity (const char *keyid, gpgme_validity_t *r_valid)
646 {
647 gpgme_error_t err = gpg_error (GPG_ERR_NO_ERROR);
648 HANDLE out;
649 const char *fmt;
650 char *p, *cmd;
651 char *uat;
652
653 *r_valid = GPGME_VALIDITY_UNKNOWN;
654 p = read_gpg_program ();
655 if (!p)
656 return gpg_error (GPG_ERR_INV_ARG);
657
658 fmt = "%s --with-colons --fixed-list-mode --list-keys \"%s\"";
659 cmd = (char*)xcalloc (1, strlen (p) + strlen (keyid) + strlen (fmt) + 2);
660 sprintf (cmd, fmt, p, keyid);
661
662 out = create_tmpfile ("gpg_keys");
663 if (create_process (cmd, NULL, out, NULL))
664 err = gpg_error (GPG_ERR_INTERNAL);
665
666 safe_free (p);
667 safe_free (cmd);
668
669 p = map_tmpfile (out, NULL);
670 if ((uat = strstr (p, "uat:"))) {
671 switch (*(uat+4)) {
672 case 'm': *r_valid = GPGME_VALIDITY_MARGINAL; break;
673 case 'f':
674 case 'u': *r_valid = GPGME_VALIDITY_FULL; break;
675 default: *r_valid = GPGME_VALIDITY_UNDEFINED; break;
676 }
677 }
678
679 safe_free (p);
680 CloseHandle (out);
681 return err;
682 }
683
684
685 static gpgme_error_t
686 clip_store_data (char *tmp_outname, DWORD outlen)
687 {
688 gpgme_data_t in;
689 gpgme_error_t err;
690
691 get_temp_name (tmp_outname, outlen-1, NULL);
692 err = gpg_data_new_from_clipboard (&in, 0);
693 if (err)
694 return err;
695 err = gpg_data_release_and_set_file (in, tmp_outname);
696 return err;
697 }
698
699
700 /* Extract all recipients from the file @file.
701 Return value: 0 on success. */
702 static gpgme_error_t
703 file_extract_recipient (const char *file, gpgme_recipient_t *r_list)
704 {
705 PACKET *pkt;
706 PKT_pubkey_enc *enc;
707 gpg_iobuf_t inp = NULL;
708 armor_filter_context_t afx;
709 gpgme_recipient_t l;
710 int rc = 0, quit=0;
711
712 if (!file || !r_list) {
713 log_debug ("do_list_packets: !r_list || !file");
714 return gpgme_error (GPG_ERR_INV_ARG);
715 }
716
717 *r_list = NULL;
718 inp = gpg_iobuf_open (file);
719 if (!inp)
720 return gpgme_err_code_from_errno (errno);
721 gpg_iobuf_ioctl (inp, 3, 1, NULL); /* disable cache */
722 if (gpg_use_armor_filter (inp)) {
723 memset (&afx, 0, sizeof (afx));
724 gpg_iobuf_push_filter (inp, gpg_armor_filter, &afx);
725 }
726 pkt = (PACKET *)xcalloc(1, sizeof *pkt);
727 gpg_init_packet (pkt);
728 while (!quit && (rc = gpg_parse_packet (inp, pkt)) != -1) {
729 switch (pkt->pkttype) {
730 case PKT_PUBKEY_ENC:
731 enc = pkt->pkt.pubkey_enc;
732 if (!enc)
733 break;
734 l = (gpgme_recipient_t)xcalloc (1, sizeof *l);
735 l->keyid = (char*)xcalloc (1, 16+1);
736 _snprintf (l->keyid, 16, "%08lX%08lX", enc->keyid[0], enc->keyid[1]);
737 l->pubkey_algo = (gpgme_pubkey_algo_t)enc->pubkey_algo;
738 l->status = 0;
739 l->next = (*r_list);
740 *r_list = l;
741 break;
742
743 case PKT_ENCRYPTED:
744 case PKT_ENCRYPTED_MDC:
745 case PKT_PLAINTEXT:
746 case PKT_COMPRESSED:
747 case PKT_PUBLIC_KEY:
748 case PKT_SECRET_KEY:
749 quit = 1;
750 break;
751
752 default:
753 break;
754 }
755 gpg_free_packet (pkt);
756 gpg_init_packet (pkt);
757 }
758 gpg_iobuf_close (inp);
759 safe_free (pkt);
760 return 0;
761 }
762
763
764 /* Either extract the list of recipients from the file @file or
765 if the string is NULL, try to extract them from the clipboard.
766 Return value: 0 on success. */
767 gpgme_error_t
768 gpg_get_recipients (const char *file, gpgme_recipient_t *r_list)
769 {
770 gpgme_error_t err;
771 char tmp[MAX_PATH+1];
772
773 if (!file) {
774 clip_store_data (tmp, DIM (tmp)-2);
775 err = file_extract_recipient (tmp, r_list);
776 DeleteFile (tmp);
777 }
778 else
779 err = file_extract_recipient (file, r_list);
780 return err;
781 }
782
783
784 /* Try to find a subpacket with the given id @subpktid
785 inside the key @key.
786 Return value: 0 on success. */
787 gpgme_error_t
788 gpg_find_key_subpacket (const char *key, int subpktid,
789 char **value)
790 {
791 gpgme_error_t err = gpg_error (GPG_ERR_NO_ERROR);
792 const char *fmt;
793 const char *spk;
794 char *p, *cmd;
795 HANDLE out;
796
797 *value = NULL;
798 p = read_gpg_program ();
799 fmt = "%s --with-colons --no-options --list-options show-sig-subpackets=%d --list-sigs %s";
800 cmd = (char*)xcalloc (1, strlen (fmt) + strlen (p) + strlen (key) + 32 + 1);
801 sprintf (cmd, fmt, p, subpktid, key);
802
803 out = create_tmpfile ("gpg_subpackets");
804 if (create_process (cmd, NULL, out, NULL))
805 err = gpg_error (GPG_ERR_INTERNAL);
806
807 safe_free (p);
808 safe_free (cmd);
809
810 p = map_tmpfile (out, NULL);
811 if (p && (spk=strstr (p, "spk"))) {
812 char *end = strstr (spk, "\n");
813
814 if (end) {
815 *value = (char*)xcalloc (1, (end-spk)+1);
816 memcpy (*value, spk, (end-spk)-1);
817 }
818 }
819
820 safe_free (p);
821 CloseHandle (out);
822 return err;
823 }

Properties

Name Value
svn:eol-style native

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26