~ubuntu-branches/ubuntu/hardy/gnupg2/hardy-proposed

« back to all changes in this revision

Viewing changes to sm/certdump.c

  • Committer: Bazaar Package Importer
  • Author(s): Michael Bienia
  • Date: 2007-05-15 13:54:55 UTC
  • mfrom: (1.1.8 upstream)
  • Revision ID: james.westby@ubuntu.com-20070515135455-89qfyalmgjy6gcqw
Tags: 2.0.4-1ubuntu1
* Merge from debian unstable, remaining changes:
  - Remove libpcsclite-dev, libopensc2-dev build dependencies (they are in
    universe).
  - Build-depend on libcurl3-gnutls-dev
  - g10/call-agent.c: set DBG_ASSUAN to 0 to suppress a debug message
  - Include /doc files as done with gnupg
  - debian/rules: add doc/com-certs.pem to the docs for gpgsm
  - debian/copyright: update download url
  - debian/README.Debian: remove note the gnupg2 isn't released yet.
  - debian/control: Change Maintainer/XSBC-Original-Maintainer field.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/* certdump.c - Dump a certificate for debugging
2
 
 *      Copyright (C) 2001, 2004 Free Software Foundation, Inc.
 
2
 * Copyright (C) 2001, 2004, 2007 Free Software Foundation, Inc.
3
3
 *
4
4
 * This file is part of GnuPG.
5
5
 *
56
56
};
57
57
 
58
58
 
59
 
/* print the first element of an S-Expression */
 
59
/* Print the first element of an S-Expression. */
60
60
void
61
 
gpgsm_print_serial (FILE *fp, ksba_const_sexp_t sn)
 
61
gpgsm_print_serial (estream_t fp, ksba_const_sexp_t sn)
62
62
{
63
63
  const char *p = (const char *)sn;
64
64
  unsigned long n;
65
65
  char *endp;
66
66
 
67
67
  if (!p)
68
 
    fputs (_("none"), fp);
 
68
    es_fputs (_("none"), fp);
69
69
  else if (*p != '(')
70
 
    fputs ("[Internal error - not an S-expression]", fp);
 
70
    es_fputs ("[Internal error - not an S-expression]", fp);
71
71
  else
72
72
    {
73
73
      p++;
74
74
      n = strtoul (p, &endp, 10);
75
75
      p = endp;
76
76
      if (*p!=':')
77
 
        fputs ("[Internal Error - invalid S-expression]", fp);
 
77
        es_fputs ("[Internal Error - invalid S-expression]", fp);
78
78
      else
79
 
        {
80
 
          for (p++; n; n--, p++)
81
 
            fprintf (fp, "%02X", *(const unsigned char*)p);
82
 
        }
 
79
        es_write_hexstring (fp, p, strlen (p), 0, NULL);
83
80
    }
84
81
}
85
82
 
148
145
 
149
146
 
150
147
void
151
 
gpgsm_print_time (FILE *fp, ksba_isotime_t t)
 
148
gpgsm_print_time (estream_t fp, ksba_isotime_t t)
152
149
{
153
150
  if (!t || !*t)
154
 
    fputs (_("none"), fp);
 
151
    es_fputs (_("none"), fp);
155
152
  else
156
 
    fprintf (fp, "%.4s-%.2s-%.2s %.2s:%.2s:%s", t, t+4, t+6, t+9, t+11, t+13);
 
153
    es_fprintf (fp, "%.4s-%.2s-%.2s %.2s:%.2s:%s",
 
154
                t, t+4, t+6, t+9, t+11, t+13);
157
155
}
158
156
 
 
157
 
159
158
void
160
159
gpgsm_dump_time (ksba_isotime_t t)
161
160
{
468
467
}
469
468
 
470
469
 
 
470
/* Print a DN part to STREAM or if STREAM is NULL to FP. */
471
471
static void
472
 
print_dn_part (FILE *fp, struct dn_array_s *dn, const char *key, int translate)
 
472
print_dn_part (FILE *fp, estream_t stream,
 
473
               struct dn_array_s *dn, const char *key, int translate)
473
474
{
474
475
  struct dn_array_s *first_dn = dn;
475
476
 
487
488
        next:
488
489
          if (!dn->done && dn->value && *dn->value)
489
490
            {
490
 
              fprintf (fp, "/%s=", dn->key);
491
 
              if (translate)
492
 
                print_sanitized_utf8_string (fp, dn->value, '/');
 
491
              if (stream)
 
492
                {
 
493
                  es_fprintf (stream, "/%s=", dn->key);
 
494
                  if (translate)
 
495
                    es_write_sanitized_utf8_buffer (stream, dn->value,
 
496
                                                    strlen (dn->value),
 
497
                                                    "/", NULL);
 
498
                  else
 
499
                    es_write_sanitized (stream, dn->value, strlen (dn->value),
 
500
                                        "/", NULL);
 
501
                }
493
502
              else
494
 
                print_sanitized_string (fp, dn->value, '/');
 
503
                {
 
504
                  fprintf (fp, "/%s=", dn->key);
 
505
                  if (translate)
 
506
                    print_sanitized_utf8_string (fp, dn->value, '/');
 
507
                  else
 
508
                    print_sanitized_string (fp, dn->value, '/');
 
509
                }
495
510
            }
496
511
          dn->done = 1;
497
512
          if (dn > first_dn && dn[-1].multivalued)
506
521
/* Print all parts of a DN in a "standard" sequence.  We first print
507
522
   all the known parts, followed by the uncommon ones */
508
523
static void
509
 
print_dn_parts (FILE *fp, struct dn_array_s *dn, int translate)
 
524
print_dn_parts (FILE *fp, estream_t stream,
 
525
                struct dn_array_s *dn, int translate)
510
526
{
511
527
  const char *stdpart[] = {
512
528
    "CN", "OU", "O", "STREET", "L", "ST", "C", "EMail", NULL 
514
530
  int i;
515
531
  
516
532
  for (i=0; stdpart[i]; i++)
517
 
      print_dn_part (fp, dn, stdpart[i], translate);
 
533
      print_dn_part (fp, stream, dn, stdpart[i], translate);
518
534
 
519
535
  /* Now print the rest without any specific ordering */
520
536
  for (; dn->key; dn++)
521
 
    print_dn_part (fp, dn, dn->key, translate);
 
537
    print_dn_part (fp, stream, dn, dn->key, translate);
522
538
}
523
539
 
524
540
 
567
583
  gcry_sexp_release (sexp);
568
584
}
569
585
 
 
586
/* Print the S-Expression in BUF to extended STREAM, which has a valid
 
587
   length of BUFLEN, as a human readable string in one line to FP. */
 
588
static void
 
589
pretty_es_print_sexp (estream_t fp, const unsigned char *buf, size_t buflen)
 
590
{
 
591
  size_t len;
 
592
  gcry_sexp_t sexp;
 
593
  char *result, *p;
 
594
 
 
595
  if ( gcry_sexp_sscan (&sexp, NULL, (const char*)buf, buflen) )
 
596
    {
 
597
      es_fputs (_("[Error - invalid encoding]"), fp);
 
598
      return;
 
599
    }
 
600
  len = gcry_sexp_sprint (sexp, GCRYSEXP_FMT_ADVANCED, NULL, 0);
 
601
  assert (len);
 
602
  result = xtrymalloc (len);
 
603
  if (!result)
 
604
    {
 
605
      es_fputs (_("[Error - out of core]"), fp);
 
606
      gcry_sexp_release (sexp);
 
607
      return;
 
608
    }
 
609
  len = gcry_sexp_sprint (sexp, GCRYSEXP_FMT_ADVANCED, result, len);
 
610
  assert (len);
 
611
  for (p = result; len; len--, p++)
 
612
    {
 
613
      if (*p == '\n')
 
614
        {
 
615
          if (len > 1) /* Avoid printing the trailing LF. */
 
616
            es_fputs ("\\n", fp);
 
617
        }
 
618
      else if (*p == '\r')
 
619
        es_fputs ("\\r", fp);
 
620
      else if (*p == '\v')
 
621
        es_fputs ("\\v", fp);
 
622
      else if (*p == '\t')
 
623
        es_fputs ("\\t", fp);
 
624
      else
 
625
        es_putc (*p, fp);
 
626
    }
 
627
  xfree (result);
 
628
  gcry_sexp_release (sexp);
 
629
}
 
630
 
 
631
 
 
632
 
570
633
 
571
634
void
572
635
gpgsm_print_name2 (FILE *fp, const char *name, int translate)
604
667
        fputs (_("[Error - invalid DN]"), fp);
605
668
      else 
606
669
        {
607
 
          print_dn_parts (fp, dn, translate);          
 
670
          print_dn_parts (fp, NULL, dn, translate);          
608
671
          for (i=0; dn[i].key; i++)
609
672
            {
610
673
              xfree (dn[i].key);
623
686
}
624
687
 
625
688
 
 
689
/* This is avariant of gpgsm_print_name sending it output to an estream. */
 
690
void
 
691
gpgsm_es_print_name (estream_t fp, const char *name)
 
692
{
 
693
  const unsigned char *s = (const unsigned char *)name;
 
694
  int i;
 
695
 
 
696
  if (!s)
 
697
    {
 
698
      es_fputs (_("[Error - No name]"), fp);
 
699
    }
 
700
  else if (*s == '<')
 
701
    {
 
702
      const char *s2 = strchr ( (char*)s+1, '>');
 
703
 
 
704
      if (s2)
 
705
        es_write_sanitized_utf8_buffer (fp, s + 1, s2 - (char*)s - 1,
 
706
                                        NULL, NULL);
 
707
    }
 
708
  else if (*s == '(')
 
709
    {
 
710
      pretty_es_print_sexp (fp, s, gcry_sexp_canon_len (s, 0, NULL, NULL));
 
711
    }
 
712
  else if (!((*s >= '0' && *s < '9')
 
713
             || (*s >= 'A' && *s <= 'Z')
 
714
             || (*s >= 'a' && *s <= 'z')))
 
715
    es_fputs (_("[Error - invalid encoding]"), fp);
 
716
  else
 
717
    {
 
718
      struct dn_array_s *dn = parse_dn (s);
 
719
 
 
720
      if (!dn)
 
721
        es_fputs (_("[Error - invalid DN]"), fp);
 
722
      else 
 
723
        {
 
724
          print_dn_parts (NULL, fp, dn, 1);          
 
725
          for (i=0; dn[i].key; i++)
 
726
            {
 
727
              xfree (dn[i].key);
 
728
              xfree (dn[i].value);
 
729
            }
 
730
          xfree (dn);
 
731
        }
 
732
    }
 
733
}
 
734
 
 
735
 
 
736
 
 
737
 
626
738
/* A cookie structure used for the memory stream. */
627
739
struct format_name_cookie 
628
740
{