~ubuntu-branches/ubuntu/oneiric/gnupg2/oneiric-updates

« back to all changes in this revision

Viewing changes to sm/export.c

  • Committer: Bazaar Package Importer
  • Author(s): Thomas Viehmann
  • Date: 2008-10-04 10:25:53 UTC
  • mfrom: (5.1.15 intrepid)
  • Revision ID: james.westby@ubuntu.com-20081004102553-fv62pp8dsitxli47
Tags: 2.0.9-3.1
* Non-maintainer upload.
* agent/gpg-agent.c: Deinit the threading library before exec'ing
  the command to run in --daemon mode. And because that still doesn't
  restore the sigprocmask, do that manually. Closes: #499569

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.
 
1
/* export.c - Export certificates and private keys.
 
2
 * Copyright (C) 2002, 2003, 2004, 2007 Free Software Foundation, Inc.
3
3
 *
4
4
 * This file is part of GnuPG.
5
5
 *
6
6
 * GnuPG is free software; you can redistribute it and/or modify
7
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
 
8
 * the Free Software Foundation; either version 3 of the License, or
9
9
 * (at your option) any later version.
10
10
 *
11
11
 * GnuPG is distributed in the hope that it will be useful,
14
14
 * GNU General Public License for more details.
15
15
 *
16
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
 
17
 * along with this program; if not, see <http://www.gnu.org/licenses/>.
19
18
 */
20
19
 
21
20
#include <config.h>
33
32
#include "keydb.h"
34
33
#include "exechelp.h"
35
34
#include "i18n.h"
 
35
#include "sysutils.h"
36
36
 
37
37
 
38
38
 
56
56
#define DUPTABLE_SIZE (1 << DUPTABLE_BITS)
57
57
 
58
58
 
59
 
static void print_short_info (ksba_cert_t cert, FILE *fp);
 
59
static void print_short_info (ksba_cert_t cert, FILE *fp, estream_t stream);
60
60
static gpg_error_t export_p12 (ctrl_t ctrl,
61
61
                               const unsigned char *certimg, size_t certimglen,
62
62
                               const char *prompt, const char *keygrip,
116
116
  /* Insert that fingerprint. */
117
117
  t = xtrymalloc (sizeof *t);
118
118
  if (!t)
119
 
    return gpg_error_from_errno (errno);
 
119
    return gpg_error_from_syserror ();
120
120
  memcpy (t->fpr, fpr+1, 19);
121
121
  t->next = table[idx];
122
122
  table[idx] = t;
126
126
 
127
127
 
128
128
 
129
 
/* Export all certificates or just those given in NAMES. */
 
129
/* Export all certificates or just those given in NAMES. If STREAM is
 
130
   not NULL the output is send to this extended stream. */
130
131
void
131
 
gpgsm_export (CTRL ctrl, STRLIST names, FILE *fp)
 
132
gpgsm_export (ctrl_t ctrl, strlist_t names, FILE *fp, estream_t stream)
132
133
{
133
134
  KEYDB_HANDLE hd = NULL;
134
135
  KEYDB_SEARCH_DESC *desc = NULL;
135
136
  int ndesc;
136
137
  Base64Context b64writer = NULL;
137
138
  ksba_writer_t writer;
138
 
  STRLIST sl;
 
139
  strlist_t sl;
139
140
  ksba_cert_t cert = NULL;
140
141
  int rc=0;
141
142
  int count = 0;
169
170
  if (!ndesc)
170
171
    {
171
172
      log_error ("allocating memory for export failed: %s\n",
172
 
                 gpg_strerror (OUT_OF_CORE (errno)));
 
173
                 gpg_strerror (out_of_core ()));
173
174
      goto leave;
174
175
    }
175
176
 
256
257
          if (ctrl->create_pem)
257
258
            {
258
259
              if (count)
 
260
                {
 
261
                  if (stream)
 
262
                    es_putc ('\n', stream);
 
263
                  else
 
264
                    putc ('\n', fp);
 
265
                }
 
266
              print_short_info (cert, fp, stream);
 
267
              if (stream)
 
268
                es_putc ('\n', stream);
 
269
              else
259
270
                putc ('\n', fp);
260
 
              print_short_info (cert, fp);
261
 
              putc ('\n', fp);
262
271
            }
263
272
          count++;
264
273
 
265
274
          if (!b64writer)
266
275
            {
267
276
              ctrl->pem_name = "CERTIFICATE";
268
 
              rc = gpgsm_create_writer (&b64writer, ctrl, fp, &writer);
 
277
              rc = gpgsm_create_writer (&b64writer, ctrl, fp, stream, &writer);
269
278
              if (rc)
270
279
                {
271
280
                  log_error ("can't create writer: %s\n", gpg_strerror (rc));
348
357
  if (!desc)
349
358
    {
350
359
      log_error ("allocating memory for export failed: %s\n",
351
 
                 gpg_strerror (OUT_OF_CORE (errno)));
 
360
                 gpg_strerror (out_of_core ()));
352
361
      goto leave;
353
362
    }
354
363
 
370
379
          log_error ("keydb_get_cert failed: %s\n", gpg_strerror (rc));
371
380
          goto leave;
372
381
        }
373
 
      
 
382
 
 
383
    next_ambiguous:      
374
384
      rc = keydb_search (hd, desc, 1);
375
385
      if (!rc)
376
 
        rc = gpg_error (GPG_ERR_AMBIGUOUS_NAME);
 
386
        {
 
387
          ksba_cert_t cert2 = NULL;
 
388
 
 
389
          if (!keydb_get_cert (hd, &cert2))
 
390
            {
 
391
              if (gpgsm_certs_identical_p (cert, cert2))
 
392
                {
 
393
                  ksba_cert_release (cert2);
 
394
                  goto next_ambiguous;
 
395
                }
 
396
              ksba_cert_release (cert2);
 
397
            }
 
398
          rc = gpg_error (GPG_ERR_AMBIGUOUS_NAME);
 
399
        }
377
400
      else if (rc == -1 || gpg_err_code (rc) == GPG_ERR_EOF)
378
401
        rc = 0;
379
402
      if (rc)
402
425
 
403
426
  if (ctrl->create_pem)
404
427
    {
405
 
      print_short_info (cert, fp);
 
428
      print_short_info (cert, fp, NULL);
406
429
      putc ('\n', fp);
407
430
    }
408
431
 
 
432
  if (opt.p12_charset && ctrl->create_pem)
 
433
    {
 
434
      fprintf (fp, "The passphrase is %s encoded.\n\n",
 
435
               opt.p12_charset);
 
436
    }
 
437
 
409
438
  ctrl->pem_name = "PKCS12";
410
 
  rc = gpgsm_create_writer (&b64writer, ctrl, fp, &writer);
 
439
  rc = gpgsm_create_writer (&b64writer, ctrl, fp, NULL, &writer);
411
440
  if (rc)
412
441
    {
413
442
      log_error ("can't create writer: %s\n", gpg_strerror (rc));
460
489
}
461
490
 
462
491
 
463
 
/* Print some info about the certifciate CERT to FP */
464
 
static void
465
 
print_short_info (ksba_cert_t cert, FILE *fp)
 
492
/* Call either es_putc or the plain putc.  */
 
493
static void
 
494
do_putc (int value, FILE *fp, estream_t stream)
 
495
{
 
496
  if (stream)
 
497
    es_putc (value, stream);
 
498
  else
 
499
    putc (value, fp);
 
500
}
 
501
 
 
502
/* Call either es_fputs or the plain fputs.  */
 
503
static void
 
504
do_fputs (const char *string, FILE *fp, estream_t stream)
 
505
{
 
506
  if (stream)
 
507
    es_fputs (string, stream);
 
508
  else
 
509
    fputs (string, fp);
 
510
}
 
511
 
 
512
 
 
513
/* Print some info about the certifciate CERT to FP or STREAM */
 
514
static void
 
515
print_short_info (ksba_cert_t cert, FILE *fp, estream_t stream)
466
516
{
467
517
  char *p;
468
518
  ksba_sexp_t sexp;
470
520
 
471
521
  for (idx=0; (p = ksba_cert_get_issuer (cert, idx)); idx++)
472
522
    {
473
 
      fputs (!idx?   "Issuer ...: "
474
 
                 : "\n   aka ...: ", fp); 
475
 
      gpgsm_print_name (fp, p);
 
523
      do_fputs ((!idx
 
524
                 ?   "Issuer ...: "
 
525
                 : "\n   aka ...: "), fp, stream); 
 
526
      if (stream)
 
527
        gpgsm_es_print_name (stream, p);
 
528
      else
 
529
        gpgsm_print_name (fp, p);
476
530
      xfree (p);
477
531
    }
478
 
  putc ('\n', fp);
 
532
  do_putc ('\n', fp, stream);
479
533
 
480
 
  fputs ("Serial ...: ", fp); 
 
534
  do_fputs ("Serial ...: ", fp, stream); 
481
535
  sexp = ksba_cert_get_serial (cert);
482
536
  if (sexp)
483
537
    {
490
544
          for (len=0; *s && *s != ':' && digitp (s); s++)
491
545
            len = len*10 + atoi_1 (s);
492
546
          if (*s == ':')
493
 
            for (s++; len; len--, s++)
494
 
              fprintf (fp, "%02X", *s);
 
547
            {
 
548
              if (stream)
 
549
                es_write_hexstring (stream, s+1, len, 0, NULL);
 
550
              else
 
551
                print_hexstring (fp, s+1, len, 0);
 
552
            }
495
553
        }
496
554
      xfree (sexp);
497
555
    }
498
 
  putc ('\n', fp);
 
556
  do_putc ('\n', fp, stream);
499
557
 
500
558
  for (idx=0; (p = ksba_cert_get_subject (cert, idx)); idx++)
501
559
    {
502
 
      fputs (!idx?   "Subject ..: "
503
 
                 : "\n    aka ..: ", fp); 
504
 
      gpgsm_print_name (fp, p);
 
560
      do_fputs ((!idx
 
561
                 ?   "Subject ..: "
 
562
                 : "\n    aka ..: "), fp, stream); 
 
563
      if (stream)
 
564
        gpgsm_es_print_name (stream, p);
 
565
      else
 
566
        gpgsm_print_name (fp, p);
505
567
      xfree (p);
506
568
    }
507
 
  putc ('\n', fp);
 
569
  do_putc ('\n', fp, stream);
508
570
}
509
571
 
510
572
 
524
586
  argv[i++] = "--prompt";
525
587
  argv[i++] = prompt?prompt:"";
526
588
  argv[i++] = "--enable-status-msg";
 
589
  if (opt.p12_charset)
 
590
    {
 
591
      argv[i++] = "--p12-charset";
 
592
      argv[i++] = opt.p12_charset;
 
593
    }
527
594
  argv[i++] = "--",
528
595
  argv[i++] = keygrip,
529
596
  argv[i] = NULL;
530
597
  assert (i < sizeof argv);
531
598
 
532
599
  return gnupg_spawn_process (pgmname, argv, infile, outfile,
533
 
                              setup_pinentry_env,
 
600
                              setup_pinentry_env, 128,
534
601
                              statusfile, pid);
535
602
}
536
603
 
550
617
  int bad_pass = 0;
551
618
 
552
619
  if (!opt.protect_tool_program || !*opt.protect_tool_program)
553
 
    pgmname = GNUPG_DEFAULT_PROTECT_TOOL;
 
620
    pgmname = gnupg_module_name (GNUPG_MODULE_NAME_PROTECT_TOOL);
554
621
  else
555
622
    pgmname = opt.protect_tool_program;
556
623
 
557
 
  infp = tmpfile ();
 
624
  infp = gnupg_tmpfile ();
558
625
  if (!infp)
559
626
    {
560
 
      err = gpg_error_from_errno (errno);
 
627
      err = gpg_error_from_syserror ();
561
628
      log_error (_("error creating temporary file: %s\n"), strerror (errno));
562
629
      goto cleanup;
563
630
    }
564
631
 
565
632
  if (fwrite (certimg, certimglen, 1, infp) != 1)
566
633
    {
567
 
      err = gpg_error_from_errno (errno);
 
634
      err = gpg_error_from_syserror ();
568
635
      log_error (_("error writing to temporary file: %s\n"),
569
636
                 strerror (errno));
570
637
      goto cleanup;
571
638
    }
572
639
 
573
 
  outfp = tmpfile ();
 
640
  outfp = gnupg_tmpfile ();
574
641
  if (!outfp)
575
642
    {
576
 
      err = gpg_error_from_errno (errno);
 
643
      err = gpg_error_from_syserror ();
577
644
      log_error (_("error creating temporary file: %s\n"), strerror (errno));
578
645
      goto cleanup;
579
646
    }
646
713
    fclose (fp);
647
714
  if (pid != -1)
648
715
    {
649
 
      if (!gnupg_wait_process (pgmname, pid))
 
716
      if (!gnupg_wait_process (pgmname, pid, NULL))
650
717
        child_err = 0;
651
718
    }
652
719
  if (!err)