~ubuntu-branches/ubuntu/precise/gnupg2/precise-proposed

« back to all changes in this revision

Viewing changes to sm/certdump.c

  • Committer: Bazaar Package Importer
  • Author(s): Eric Dorland
  • Date: 2009-03-08 22:46:47 UTC
  • mfrom: (1.1.11 upstream)
  • Revision ID: james.westby@ubuntu.com-20090308224647-gq17gatcl71lrc2k
Tags: 2.0.11-1
* New upstream release. (Closes: #496663)
* debian/control: Make the description a little more distinctive than
  gnupg v1's. Thanks Jari Aalto. (Closes: #496323)

Show diffs side-by-side

added added

removed removed

Lines of Context:
39
39
#include "keydb.h"
40
40
#include "i18n.h"
41
41
 
42
 
#ifdef HAVE_FOPENCOOKIE
43
 
typedef ssize_t my_funopen_hook_ret_t;
44
 
typedef size_t  my_funopen_hook_size_t;
45
 
#else
46
 
typedef int     my_funopen_hook_ret_t;
47
 
typedef int     my_funopen_hook_size_t;
48
 
#endif
49
 
 
50
42
 
51
43
struct dn_array_s {
52
44
  char *key;
719
711
}
720
712
 
721
713
 
722
 
/* This is avariant of gpgsm_print_name sending it output to an estream. */
 
714
/* This is a variant of gpgsm_print_name sending it output to an estream. */
723
715
void
724
 
gpgsm_es_print_name (estream_t fp, const char *name)
 
716
gpgsm_es_print_name2 (estream_t fp, const char *name, int translate)
725
717
{
726
718
  const unsigned char *s = (const unsigned char *)name;
727
719
  int i;
735
727
      const char *s2 = strchr ( (char*)s+1, '>');
736
728
 
737
729
      if (s2)
738
 
        es_write_sanitized_utf8_buffer (fp, s + 1, s2 - (char*)s - 1,
739
 
                                        NULL, NULL);
 
730
        {
 
731
          if (translate)
 
732
            es_write_sanitized_utf8_buffer (fp, s + 1, s2 - (char*)s - 1,
 
733
                                            NULL, NULL);
 
734
          else
 
735
            es_write_sanitized (fp, s + 1, s2 - (char*)s - 1, NULL, NULL);
 
736
        }
740
737
    }
741
738
  else if (*s == '(')
742
739
    {
754
751
        es_fputs (_("[Error - invalid DN]"), fp);
755
752
      else 
756
753
        {
757
 
          print_dn_parts (NULL, fp, dn, 1);          
 
754
          print_dn_parts (NULL, fp, dn, translate);          
758
755
          for (i=0; dn[i].key; i++)
759
756
            {
760
757
              xfree (dn[i].key);
766
763
}
767
764
 
768
765
 
769
 
 
770
 
 
771
 
#if defined (HAVE_FOPENCOOKIE) || defined (HAVE_FUNOPEN)
 
766
void
 
767
gpgsm_es_print_name (estream_t fp, const char *name)
 
768
{
 
769
  gpgsm_es_print_name2 (fp, name, 1);
 
770
}
 
771
 
 
772
 
772
773
/* A cookie structure used for the memory stream. */
773
774
struct format_name_cookie 
774
775
{
779
780
};
780
781
 
781
782
/* The writer function for the memory stream. */
782
 
static my_funopen_hook_ret_t
783
 
format_name_writer (void *cookie, const char *buffer,
784
 
                    my_funopen_hook_size_t size)
 
783
static ssize_t
 
784
format_name_writer (void *cookie, const void *buffer, size_t size)
785
785
{
786
786
  struct format_name_cookie *c = cookie;
787
787
  char *p;
788
788
 
789
 
  if (c->buffer)
790
 
    p = xtryrealloc (c->buffer, c->size + size + 1);
 
789
  if (!c->buffer)
 
790
    {
 
791
      p = xtrymalloc (size + 1 + 1);
 
792
      if (p)
 
793
        {
 
794
          c->size = size + 1;
 
795
          c->buffer = p;
 
796
          c->len = 0;
 
797
        }
 
798
    }
 
799
  else if (c->len + size < c->len)
 
800
    {
 
801
      p = NULL;
 
802
      errno = ENOMEM;
 
803
    }
 
804
  else if (c->size < c->len + size)
 
805
    {
 
806
      p = xtryrealloc (c->buffer, c->len + size + 1);
 
807
      if (p)
 
808
        {
 
809
          c->size = c->len + size;
 
810
          c->buffer = p;
 
811
        }
 
812
    }
791
813
  else
792
 
    p = xtrymalloc (size + 1);
 
814
    p = c->buffer;
793
815
  if (!p)
794
816
    {
795
817
      c->error = errno;
796
818
      xfree (c->buffer);
 
819
      c->buffer = NULL;
797
820
      errno = c->error;
798
 
      return (my_funopen_hook_ret_t)(-1);
 
821
      return -1;
799
822
    }
800
 
  c->buffer = p;
801
823
  memcpy (p + c->len, buffer, size);
802
824
  c->len += size;
803
825
  p[c->len] = 0; /* Terminate string. */ 
804
826
 
805
 
  return (my_funopen_hook_ret_t)size;
 
827
  return (ssize_t)size;
806
828
}
807
 
#endif /*HAVE_FOPENCOOKIE || HAVE_FUNOPEN*/
808
829
 
809
830
 
810
831
/* Format NAME which is expected to be in rfc2253 format into a better
815
836
char *
816
837
gpgsm_format_name2 (const char *name, int translate)
817
838
{
818
 
#if defined (HAVE_FOPENCOOKIE) || defined (HAVE_FUNOPEN)
819
 
  FILE *fp;
 
839
  estream_t fp;
820
840
  struct format_name_cookie cookie;
 
841
  es_cookie_io_functions_t io = { NULL };
821
842
 
822
843
  memset (&cookie, 0, sizeof cookie);
823
844
 
824
 
#ifdef HAVE_FOPENCOOKIE
825
 
  {
826
 
    cookie_io_functions_t io = { NULL };
827
 
    io.write = format_name_writer;
828
 
    
829
 
    fp = fopencookie (&cookie, "w", io);
830
 
  }
831
 
#else /*!HAVE_FOPENCOOKIE*/
832
 
  {
833
 
    fp = funopen (&cookie, NULL, format_name_writer, NULL, NULL);
834
 
  }
835
 
#endif /*!HAVE_FOPENCOOKIE*/
 
845
  io.func_write = format_name_writer;
 
846
  fp = es_fopencookie (&cookie, "w", io);
836
847
  if (!fp)
837
848
    {
838
849
      int save_errno = errno;
840
851
      errno = save_errno;
841
852
      return NULL;
842
853
    }
843
 
  gpgsm_print_name2 (fp, name, translate);
844
 
  fclose (fp);
 
854
  gpgsm_es_print_name2 (fp, name, translate);
 
855
  es_fclose (fp);
845
856
  if (cookie.error || !cookie.buffer)
846
857
    {
847
858
      xfree (cookie.buffer);
849
860
      return NULL;
850
861
    }
851
862
  return cookie.buffer;
852
 
#else /* No fun - use the name verbatim. */
853
 
  return xtrystrdup (name);
854
 
#endif /* No fun. */
855
863
}
856
864
 
 
865
 
857
866
char *
858
867
gpgsm_format_name (const char *name)
859
868
{
915
924
 
916
925
 
917
926
/* Create a key description for the CERT, this may be passed to the
918
 
   pinentry.  The caller must free the returned string. NULL may be
 
927
   pinentry.  The caller must free the returned string.  NULL may be
919
928
   returned on error. */
920
929
char *
921
930
gpgsm_format_keydesc (ksba_cert_t cert)
922
931
{
923
 
  int rc;
924
 
  char *name, *subject, *buffer, *p;
925
 
  const char *s;
 
932
  char *name, *subject, *buffer;
926
933
  ksba_isotime_t t;
927
934
  char created[20];
928
935
  char expires[20];
951
958
 
952
959
  orig_codeset = i18n_switchto_utf8 ();
953
960
 
954
 
  rc = asprintf (&name,
955
 
                 _("Please enter the passphrase to unlock the"
956
 
                   " secret key for the X.509 certificate:\n"
957
 
                   "\"%s\"\n"
958
 
                   "S/N %s, ID 0x%08lX,\n"
959
 
                   "created %s, expires %s.\n" ),
960
 
                 subject? subject:"?",
961
 
                 sn? sn: "?",
962
 
                 gpgsm_get_short_fingerprint (cert),
963
 
                 created, expires);
964
 
 
 
961
  name = xtryasprintf (_("Please enter the passphrase to unlock the"
 
962
                         " secret key for the X.509 certificate:\n"
 
963
                         "\"%s\"\n"
 
964
                         "S/N %s, ID 0x%08lX,\n"
 
965
                         "created %s, expires %s.\n" ),
 
966
                       subject? subject:"?",
 
967
                       sn? sn: "?",
 
968
                       gpgsm_get_short_fingerprint (cert),
 
969
                       created, expires);
 
970
  
965
971
  i18n_switchback (orig_codeset);
966
 
 
967
 
  if (rc < 0)
 
972
  
 
973
  if (!name)
968
974
    {
969
 
      int save_errno = errno;
970
975
      xfree (subject);
971
976
      xfree (sn);
972
 
      errno = save_errno;
973
977
      return NULL;
974
978
    }
975
979
  
976
980
  xfree (subject);
977
981
  xfree (sn);
978
982
 
979
 
  buffer = p = xtrymalloc (strlen (name) * 3 + 1);
980
 
  for (s=name; *s; s++)
981
 
    {
982
 
      if (*s < ' ' || *s == '+')
983
 
        {
984
 
          sprintf (p, "%%%02X", *(unsigned char *)s);
985
 
          p += 3;
986
 
        }
987
 
      else if (*s == ' ')
988
 
        *p++ = '+';
989
 
      else
990
 
        *p++ = *s;
991
 
    }
992
 
  *p = 0;
993
 
  free (name); 
994
 
 
 
983
  buffer = percent_plus_escape (name);
 
984
  xfree (name); 
995
985
  return buffer;
996
986
}
997
987