~ubuntu-branches/ubuntu/dapper/gnupg2/dapper

« back to all changes in this revision

Viewing changes to sm/export.c

  • Committer: Bazaar Package Importer
  • Author(s): Andreas Mueller
  • Date: 2005-03-29 10:30:32 UTC
  • Revision ID: james.westby@ubuntu.com-20050329103032-sj42n2ain3ipx310
Tags: upstream-1.9.15
ImportĀ upstreamĀ versionĀ 1.9.15

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* export.c
 
2
 * Copyright (C) 2002, 2003, 2004 Free Software Foundation, Inc.
 
3
 *
 
4
 * This file is part of GnuPG.
 
5
 *
 
6
 * GnuPG is free software; you can redistribute it and/or modify
 
7
 * it under the terms of the GNU General Public License as published by
 
8
 * the Free Software Foundation; either version 2 of the License, or
 
9
 * (at your option) any later version.
 
10
 *
 
11
 * GnuPG is distributed in the hope that it will be useful,
 
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
14
 * GNU General Public License for more details.
 
15
 *
 
16
 * You should have received a copy of the GNU General Public License
 
17
 * along with this program; if not, write to the Free Software
 
18
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
 
19
 */
 
20
 
 
21
#include <config.h>
 
22
#include <stdio.h>
 
23
#include <stdlib.h>
 
24
#include <string.h>
 
25
#include <errno.h>
 
26
#include <time.h>
 
27
#include <assert.h>
 
28
 
 
29
#include "gpgsm.h"
 
30
#include <gcrypt.h>
 
31
#include <ksba.h>
 
32
 
 
33
#include "keydb.h"
 
34
#include "exechelp.h"
 
35
#include "i18n.h"
 
36
 
 
37
 
 
38
 
 
39
/* A table to store a fingerprint as used in a duplicates table.  We
 
40
   don't need to hash here because a fingerprint is alrady a perfect
 
41
   hash value.  This we use the most significant bits to index the
 
42
   table and then use a linked list for the overflow.  Possible
 
43
   enhancement for very large number of certictates: Add a second
 
44
   level table and then resort to a linked list. */
 
45
struct duptable_s
 
46
{
 
47
  struct duptable_s *next;
 
48
 
 
49
  /* Note that we only need to store 19 bytes because the first byte
 
50
     is implictly given by the table index (we require at least 8
 
51
     bits). */
 
52
  unsigned char fpr[19];
 
53
};
 
54
typedef struct duptable_s *duptable_t;
 
55
#define DUPTABLE_BITS 12
 
56
#define DUPTABLE_SIZE (1 << DUPTABLE_BITS)
 
57
 
 
58
 
 
59
static void print_short_info (ksba_cert_t cert, FILE *fp);
 
60
static gpg_error_t export_p12 (ctrl_t ctrl,
 
61
                               const unsigned char *certimg, size_t certimglen,
 
62
                               const char *prompt, const char *keygrip,
 
63
                               FILE **retfp);
 
64
 
 
65
 
 
66
/* Create a table used to indetify duplicated certificates. */
 
67
static duptable_t *
 
68
create_duptable (void)
 
69
{
 
70
  return xtrycalloc (DUPTABLE_SIZE, sizeof (duptable_t));
 
71
}
 
72
 
 
73
static void
 
74
destroy_duptable (duptable_t *table)
 
75
{
 
76
  int idx;
 
77
  duptable_t t, t2;
 
78
 
 
79
  if (table)
 
80
    {
 
81
      for (idx=0; idx < DUPTABLE_SIZE; idx++) 
 
82
        for (t = table[idx]; t; t = t2)
 
83
          {
 
84
            t2 = t->next;
 
85
            xfree (t);
 
86
          }
 
87
      xfree (table);
 
88
    }
 
89
}
 
90
 
 
91
/* Insert the 20 byte fingerprint FPR into TABLE.  Sets EXITS to true
 
92
   if the fingerprint already exists in the table. */
 
93
static gpg_error_t
 
94
insert_duptable (duptable_t *table, unsigned char *fpr, int *exists)
 
95
{
 
96
  size_t idx;
 
97
  duptable_t t;
 
98
  
 
99
  *exists = 0;
 
100
  idx = fpr[0];
 
101
#if DUPTABLE_BITS > 16 || DUPTABLE_BITS < 8
 
102
#error cannot handle a table larger than 16 bits or smaller than 8 bits
 
103
#elif DUPTABLE_BITS > 8
 
104
  idx <<= (DUPTABLE_BITS - 8);  
 
105
  idx |= (fpr[1] & ~(~0 << 4)); 
 
106
#endif  
 
107
 
 
108
  for (t = table[idx]; t; t = t->next)
 
109
    if (!memcmp (t->fpr, fpr+1, 19))
 
110
      break;
 
111
  if (t)
 
112
    {
 
113
      *exists = 1;
 
114
      return 0;
 
115
    }
 
116
  /* Insert that fingerprint. */
 
117
  t = xtrymalloc (sizeof *t);
 
118
  if (!t)
 
119
    return gpg_error_from_errno (errno);
 
120
  memcpy (t->fpr, fpr+1, 19);
 
121
  t->next = table[idx];
 
122
  table[idx] = t;
 
123
  return 0;
 
124
}
 
125
 
 
126
 
 
127
 
 
128
 
 
129
/* Export all certificates or just those given in NAMES. */
 
130
void
 
131
gpgsm_export (CTRL ctrl, STRLIST names, FILE *fp)
 
132
{
 
133
  KEYDB_HANDLE hd = NULL;
 
134
  KEYDB_SEARCH_DESC *desc = NULL;
 
135
  int ndesc;
 
136
  Base64Context b64writer = NULL;
 
137
  ksba_writer_t writer;
 
138
  STRLIST sl;
 
139
  ksba_cert_t cert = NULL;
 
140
  int rc=0;
 
141
  int count = 0;
 
142
  int i;
 
143
  duptable_t *dtable;
 
144
 
 
145
  
 
146
  dtable = create_duptable ();
 
147
  if (!dtable)
 
148
    {
 
149
      log_error ("creating duplicates table failed: %s\n", strerror (errno));
 
150
      goto leave;
 
151
    }
 
152
 
 
153
  hd = keydb_new (0);
 
154
  if (!hd)
 
155
    {
 
156
      log_error ("keydb_new failed\n");
 
157
      goto leave;
 
158
    }
 
159
 
 
160
  if (!names)
 
161
    ndesc = 1;
 
162
  else
 
163
    {
 
164
      for (sl=names, ndesc=0; sl; sl = sl->next, ndesc++) 
 
165
        ;
 
166
    }
 
167
 
 
168
  desc = xtrycalloc (ndesc, sizeof *desc);
 
169
  if (!ndesc)
 
170
    {
 
171
      log_error ("allocating memory for export failed: %s\n",
 
172
                 gpg_strerror (OUT_OF_CORE (errno)));
 
173
      goto leave;
 
174
    }
 
175
 
 
176
  if (!names)
 
177
    desc[0].mode = KEYDB_SEARCH_MODE_FIRST;
 
178
  else 
 
179
    {
 
180
      for (ndesc=0, sl=names; sl; sl = sl->next) 
 
181
        {
 
182
          rc = keydb_classify_name (sl->d, desc+ndesc);
 
183
          if (rc)
 
184
            {
 
185
              log_error ("key `%s' not found: %s\n",
 
186
                         sl->d, gpg_strerror (rc));
 
187
              rc = 0;
 
188
            }
 
189
          else
 
190
            ndesc++;
 
191
        }
 
192
    }
 
193
 
 
194
  /* If all specifications are done by fingerprint, we switch to
 
195
     ephemeral mode so that _all_ currently available and matching
 
196
     certificates are exported. 
 
197
 
 
198
     fixme: we should in this case keep a list of certificates to
 
199
     avoid accidential export of duplicate certificates. */
 
200
  if (names && ndesc)
 
201
    {
 
202
      for (i=0; (i < ndesc
 
203
                 && (desc[i].mode == KEYDB_SEARCH_MODE_FPR
 
204
                     || desc[i].mode == KEYDB_SEARCH_MODE_FPR20
 
205
                     || desc[i].mode == KEYDB_SEARCH_MODE_FPR16)); i++)
 
206
        ;
 
207
      if (i == ndesc)
 
208
        keydb_set_ephemeral (hd, 1);
 
209
    }
 
210
      
 
211
  while (!(rc = keydb_search (hd, desc, ndesc)))
 
212
    {
 
213
      unsigned char fpr[20];
 
214
      int exists;
 
215
 
 
216
      if (!names) 
 
217
        desc[0].mode = KEYDB_SEARCH_MODE_NEXT;
 
218
 
 
219
      rc = keydb_get_cert (hd, &cert);
 
220
      if (rc) 
 
221
        {
 
222
          log_error ("keydb_get_cert failed: %s\n", gpg_strerror (rc));
 
223
          goto leave;
 
224
        }
 
225
 
 
226
      gpgsm_get_fingerprint (cert, 0, fpr, NULL);
 
227
      rc = insert_duptable (dtable, fpr, &exists);
 
228
      if (rc)
 
229
        {
 
230
          log_error ("inserting into duplicates table fauiled: %s\n",
 
231
                     gpg_strerror (rc));
 
232
          goto leave;
 
233
        }
 
234
 
 
235
      if (!exists && count && !ctrl->create_pem)
 
236
        {
 
237
          log_info ("exporting more than one certificate "
 
238
                    "is not possible in binary mode\n");
 
239
          log_info ("ignoring other certificates\n");
 
240
          break;
 
241
        }
 
242
 
 
243
      if (!exists)
 
244
        {
 
245
          const unsigned char *image;
 
246
          size_t imagelen;
 
247
 
 
248
          image = ksba_cert_get_image (cert, &imagelen);
 
249
          if (!image)
 
250
            {
 
251
              log_error ("ksba_cert_get_image failed\n");
 
252
              goto leave;
 
253
            }
 
254
 
 
255
 
 
256
          if (ctrl->create_pem)
 
257
            {
 
258
              if (count)
 
259
                putc ('\n', fp);
 
260
              print_short_info (cert, fp);
 
261
              putc ('\n', fp);
 
262
            }
 
263
          count++;
 
264
 
 
265
          if (!b64writer)
 
266
            {
 
267
              ctrl->pem_name = "CERTIFICATE";
 
268
              rc = gpgsm_create_writer (&b64writer, ctrl, fp, &writer);
 
269
              if (rc)
 
270
                {
 
271
                  log_error ("can't create writer: %s\n", gpg_strerror (rc));
 
272
                  goto leave;
 
273
                }
 
274
            }
 
275
 
 
276
          rc = ksba_writer_write (writer, image, imagelen);
 
277
          if (rc)
 
278
            {
 
279
              log_error ("write error: %s\n", gpg_strerror (rc));
 
280
              goto leave;
 
281
            }
 
282
 
 
283
          if (ctrl->create_pem)
 
284
            {
 
285
              /* We want one certificate per PEM block */
 
286
              rc = gpgsm_finish_writer (b64writer);
 
287
              if (rc) 
 
288
                {
 
289
                  log_error ("write failed: %s\n", gpg_strerror (rc));
 
290
                  goto leave;
 
291
                }
 
292
              gpgsm_destroy_writer (b64writer);
 
293
              b64writer = NULL;
 
294
            }
 
295
        }
 
296
 
 
297
      ksba_cert_release (cert); 
 
298
      cert = NULL;
 
299
    }
 
300
  if (rc && rc != -1)
 
301
    log_error ("keydb_search failed: %s\n", gpg_strerror (rc));
 
302
  else if (b64writer)
 
303
    {
 
304
      rc = gpgsm_finish_writer (b64writer);
 
305
      if (rc) 
 
306
        {
 
307
          log_error ("write failed: %s\n", gpg_strerror (rc));
 
308
          goto leave;
 
309
        }
 
310
    }
 
311
  
 
312
 leave:
 
313
  gpgsm_destroy_writer (b64writer);
 
314
  ksba_cert_release (cert);
 
315
  xfree (desc);
 
316
  keydb_release (hd);
 
317
  destroy_duptable (dtable);
 
318
}
 
319
 
 
320
 
 
321
/* Export a certificates and its private key. */
 
322
void
 
323
gpgsm_p12_export (ctrl_t ctrl, const char *name, FILE *fp)
 
324
{
 
325
  KEYDB_HANDLE hd;
 
326
  KEYDB_SEARCH_DESC *desc = NULL;
 
327
  Base64Context b64writer = NULL;
 
328
  ksba_writer_t writer;
 
329
  ksba_cert_t cert = NULL;
 
330
  int rc=0;
 
331
  const unsigned char *image;
 
332
  size_t imagelen;
 
333
  char *keygrip = NULL;
 
334
  char *prompt;
 
335
  char buffer[1024];
 
336
  int  nread;
 
337
  FILE *datafp = NULL;
 
338
 
 
339
 
 
340
  hd = keydb_new (0);
 
341
  if (!hd)
 
342
    {
 
343
      log_error ("keydb_new failed\n");
 
344
      goto leave;
 
345
    }
 
346
 
 
347
  desc = xtrycalloc (1, sizeof *desc);
 
348
  if (!desc)
 
349
    {
 
350
      log_error ("allocating memory for export failed: %s\n",
 
351
                 gpg_strerror (OUT_OF_CORE (errno)));
 
352
      goto leave;
 
353
    }
 
354
 
 
355
  rc = keydb_classify_name (name, desc);
 
356
  if (rc)
 
357
    {
 
358
      log_error ("key `%s' not found: %s\n",
 
359
                 name, gpg_strerror (rc));
 
360
      goto leave;
 
361
    }
 
362
 
 
363
  /* Lookup the certificate an make sure that it is unique. */
 
364
  rc = keydb_search (hd, desc, 1);
 
365
  if (!rc)
 
366
    {
 
367
      rc = keydb_get_cert (hd, &cert);
 
368
      if (rc) 
 
369
        {
 
370
          log_error ("keydb_get_cert failed: %s\n", gpg_strerror (rc));
 
371
          goto leave;
 
372
        }
 
373
      
 
374
      rc = keydb_search (hd, desc, 1);
 
375
      if (!rc)
 
376
        rc = gpg_error (GPG_ERR_AMBIGUOUS_NAME);
 
377
      else if (rc == -1 || gpg_err_code (rc) == GPG_ERR_EOF)
 
378
        rc = 0;
 
379
      if (rc)
 
380
        {
 
381
          log_error ("key `%s' not found: %s\n",
 
382
                     name, gpg_strerror (rc));
 
383
          goto leave;
 
384
        }
 
385
    }
 
386
      
 
387
  keygrip = gpgsm_get_keygrip_hexstring (cert);
 
388
  if (!keygrip || gpgsm_agent_havekey (ctrl, keygrip))
 
389
    {
 
390
      /* Note, that the !keygrip case indicates a bad certificate. */
 
391
      rc = gpg_error (GPG_ERR_NO_SECKEY);
 
392
      log_error ("can't export key `%s': %s\n", name, gpg_strerror (rc));
 
393
      goto leave;
 
394
    }
 
395
  
 
396
  image = ksba_cert_get_image (cert, &imagelen);
 
397
  if (!image)
 
398
    {
 
399
      log_error ("ksba_cert_get_image failed\n");
 
400
      goto leave;
 
401
    }
 
402
 
 
403
  if (ctrl->create_pem)
 
404
    {
 
405
      print_short_info (cert, fp);
 
406
      putc ('\n', fp);
 
407
    }
 
408
 
 
409
  ctrl->pem_name = "PKCS12";
 
410
  rc = gpgsm_create_writer (&b64writer, ctrl, fp, &writer);
 
411
  if (rc)
 
412
    {
 
413
      log_error ("can't create writer: %s\n", gpg_strerror (rc));
 
414
      goto leave;
 
415
    }
 
416
 
 
417
 
 
418
  prompt = gpgsm_format_keydesc (cert);
 
419
  rc = export_p12 (ctrl, image, imagelen, prompt, keygrip, &datafp);
 
420
  xfree (prompt);
 
421
  if (rc)
 
422
    goto leave;
 
423
  rewind (datafp);
 
424
  while ( (nread = fread (buffer, 1, sizeof buffer, datafp)) > 0 )
 
425
    if ((rc = ksba_writer_write (writer, buffer, nread)))
 
426
      {
 
427
        log_error ("write failed: %s\n", gpg_strerror (rc));
 
428
        goto leave;
 
429
      }
 
430
  if (ferror (datafp))
 
431
    {
 
432
      rc = gpg_error_from_errno (rc);
 
433
      log_error ("error reading temporary file: %s\n", gpg_strerror (rc));
 
434
      goto leave;
 
435
    }
 
436
 
 
437
  if (ctrl->create_pem)
 
438
    {
 
439
      /* We want one certificate per PEM block */
 
440
      rc = gpgsm_finish_writer (b64writer);
 
441
      if (rc) 
 
442
        {
 
443
          log_error ("write failed: %s\n", gpg_strerror (rc));
 
444
          goto leave;
 
445
        }
 
446
      gpgsm_destroy_writer (b64writer);
 
447
      b64writer = NULL;
 
448
    }
 
449
  
 
450
  ksba_cert_release (cert); 
 
451
  cert = NULL;
 
452
 
 
453
 leave:
 
454
  if (datafp)
 
455
    fclose (datafp);
 
456
  gpgsm_destroy_writer (b64writer);
 
457
  ksba_cert_release (cert);
 
458
  xfree (desc);
 
459
  keydb_release (hd);
 
460
}
 
461
 
 
462
 
 
463
/* Print some info about the certifciate CERT to FP */
 
464
static void
 
465
print_short_info (ksba_cert_t cert, FILE *fp)
 
466
{
 
467
  char *p;
 
468
  ksba_sexp_t sexp;
 
469
  int idx;
 
470
 
 
471
  for (idx=0; (p = ksba_cert_get_issuer (cert, idx)); idx++)
 
472
    {
 
473
      fputs (!idx?   "Issuer ...: "
 
474
                 : "\n   aka ...: ", fp); 
 
475
      gpgsm_print_name (fp, p);
 
476
      xfree (p);
 
477
    }
 
478
  putc ('\n', fp);
 
479
 
 
480
  fputs ("Serial ...: ", fp); 
 
481
  sexp = ksba_cert_get_serial (cert);
 
482
  if (sexp)
 
483
    {
 
484
      int len;
 
485
      const unsigned char *s = sexp;
 
486
      
 
487
      if (*s == '(')
 
488
        {
 
489
          s++;
 
490
          for (len=0; *s && *s != ':' && digitp (s); s++)
 
491
            len = len*10 + atoi_1 (s);
 
492
          if (*s == ':')
 
493
            for (s++; len; len--, s++)
 
494
              fprintf (fp, "%02X", *s);
 
495
        }
 
496
      xfree (sexp);
 
497
    }
 
498
  putc ('\n', fp);
 
499
 
 
500
  for (idx=0; (p = ksba_cert_get_subject (cert, idx)); idx++)
 
501
    {
 
502
      fputs (!idx?   "Subject ..: "
 
503
                 : "\n    aka ..: ", fp); 
 
504
      gpgsm_print_name (fp, p);
 
505
      xfree (p);
 
506
    }
 
507
  putc ('\n', fp);
 
508
}
 
509
 
 
510
 
 
511
static gpg_error_t
 
512
popen_protect_tool (const char *pgmname,
 
513
                    FILE *infile, FILE *outfile, FILE **statusfile, 
 
514
                    const char *prompt, const char *keygrip,
 
515
                    pid_t *pid)
 
516
{
 
517
  const char *argv[20];
 
518
  int i=0;
 
519
 
 
520
  argv[i++] = "--homedir";
 
521
  argv[i++] = opt.homedir;
 
522
  argv[i++] = "--p12-export";
 
523
  argv[i++] = "--prompt";
 
524
  argv[i++] = prompt?prompt:"";
 
525
  argv[i++] = "--enable-status-msg";
 
526
  argv[i++] = "--",
 
527
  argv[i++] = keygrip,
 
528
  argv[i] = NULL;
 
529
  assert (i < sizeof argv);
 
530
 
 
531
  return gnupg_spawn_process (pgmname, argv, infile, outfile,
 
532
                              setup_pinentry_env,
 
533
                              statusfile, pid);
 
534
}
 
535
 
 
536
 
 
537
static gpg_error_t
 
538
export_p12 (ctrl_t ctrl, const unsigned char *certimg, size_t certimglen,
 
539
            const char *prompt, const char *keygrip,
 
540
            FILE **retfp)
 
541
{
 
542
  const char *pgmname;
 
543
  gpg_error_t err = 0, child_err = 0;
 
544
  int c, cont_line;
 
545
  unsigned int pos;
 
546
  FILE *infp = NULL, *outfp = NULL, *fp = NULL;
 
547
  char buffer[1024];
 
548
  pid_t pid = -1;
 
549
  int bad_pass = 0;
 
550
 
 
551
  if (!opt.protect_tool_program || !*opt.protect_tool_program)
 
552
    pgmname = GNUPG_DEFAULT_PROTECT_TOOL;
 
553
  else
 
554
    pgmname = opt.protect_tool_program;
 
555
 
 
556
  infp = tmpfile ();
 
557
  if (!infp)
 
558
    {
 
559
      err = gpg_error_from_errno (errno);
 
560
      log_error (_("error creating temporary file: %s\n"), strerror (errno));
 
561
      goto cleanup;
 
562
    }
 
563
 
 
564
  if (fwrite (certimg, certimglen, 1, infp) != 1)
 
565
    {
 
566
      err = gpg_error_from_errno (errno);
 
567
      log_error (_("error writing to temporary file: %s\n"),
 
568
                 strerror (errno));
 
569
      goto cleanup;
 
570
    }
 
571
 
 
572
  outfp = tmpfile ();
 
573
  if (!outfp)
 
574
    {
 
575
      err = gpg_error_from_errno (errno);
 
576
      log_error (_("error creating temporary file: %s\n"), strerror (errno));
 
577
      goto cleanup;
 
578
    }
 
579
 
 
580
  err = popen_protect_tool (pgmname, infp, outfp, &fp, prompt, keygrip, &pid);
 
581
  if (err)
 
582
    {
 
583
      pid = -1;
 
584
      goto cleanup;
 
585
    }
 
586
  fclose (infp);
 
587
  infp = NULL;
 
588
 
 
589
  /* Read stderr of the protect tool. */
 
590
  pos = 0;
 
591
  cont_line = 0;
 
592
  while ((c=getc (fp)) != EOF)
 
593
    {
 
594
      /* fixme: We could here grep for status information of the
 
595
         protect tool to figure out better error codes for
 
596
         CHILD_ERR. */
 
597
      buffer[pos++] = c;
 
598
      if (pos >= sizeof buffer - 5 || c == '\n')
 
599
        {
 
600
          buffer[pos - (c == '\n')] = 0;
 
601
          if (cont_line)
 
602
            log_printf ("%s", buffer);
 
603
          else
 
604
            {
 
605
              if (!strncmp (buffer, "gpg-protect-tool: [PROTECT-TOOL:] ",34))
 
606
                {
 
607
                  char *p, *pend;
 
608
 
 
609
                  p = buffer + 34;
 
610
                  pend = strchr (p, ' ');
 
611
                  if (pend)
 
612
                    *pend = 0;
 
613
                  if ( !strcmp (p, "bad-passphrase"))
 
614
                    bad_pass++;
 
615
                }
 
616
              else 
 
617
                log_info ("%s", buffer);
 
618
            }
 
619
          pos = 0;
 
620
          cont_line = (c != '\n');
 
621
        }
 
622
    }
 
623
 
 
624
  if (pos)
 
625
    {
 
626
      buffer[pos] = 0;
 
627
      if (cont_line)
 
628
        log_printf ("%s\n", buffer);
 
629
      else
 
630
        log_info ("%s\n", buffer);
 
631
    }
 
632
  else if (cont_line)
 
633
    log_printf ("\n");
 
634
 
 
635
  /* If we found no error in the output of the child, setup a suitable
 
636
     error code, which will later be reset if the exit status of the
 
637
     child is 0. */
 
638
  if (!child_err)
 
639
    child_err = gpg_error (GPG_ERR_DECRYPT_FAILED);
 
640
 
 
641
 cleanup:
 
642
  if (infp)
 
643
    fclose (infp);
 
644
  if (fp)
 
645
    fclose (fp);
 
646
  if (pid != -1)
 
647
    {
 
648
      if (!gnupg_wait_process (pgmname, pid))
 
649
        child_err = 0;
 
650
    }
 
651
  if (!err)
 
652
    err = child_err;
 
653
  if (err)
 
654
    {
 
655
      if (outfp)
 
656
        fclose (outfp);
 
657
    }
 
658
  else
 
659
    *retfp = outfp;
 
660
  if (bad_pass)
 
661
    {
 
662
      /* During export this is the passphrase used to unprotect the
 
663
         key and not the pkcs#12 thing as in export.  Therefore we can
 
664
         issue the regular passphrase status.  FIXME: replace the all
 
665
         zero keyid by a regular one. */
 
666
      gpgsm_status (ctrl, STATUS_BAD_PASSPHRASE, "0000000000000000");
 
667
    }
 
668
  return err;
 
669
}
 
670