aboutsummaryrefslogtreecommitdiffstats
path: root/sm/keylist.c
blob: e200c228ee7903870c4864f921c44271ec376d79 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
/* keylist.c
 * Copyright (C) 1998, 1999, 2000, 2001 Free Software Foundation, Inc.
 *
 * This file is part of GnuPG.
 *
 * GnuPG is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * GnuPG is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
 */

#include <config.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <unistd.h> 
#include <time.h>
#include <assert.h>

#include <gcrypt.h>
#include <ksba.h>

#include "gpgsm.h"
#include "keydb.h"




static void
print_key_data (KsbaCert cert, FILE *fp)
{
#if 0  
  int n = pk ? pubkey_get_npkey( pk->pubkey_algo ) : 0;
  int i;

  for(i=0; i < n; i++ ) 
    {
      fprintf (fp, "pkd:%d:%u:", i, mpi_get_nbits( pk->pkey[i] ) );
      mpi_print(stdout, pk->pkey[i], 1 );
      putchar(':');
      putchar('\n');
    }
#endif
}

static void
print_capabilities (KsbaCert cert, FILE *fp)
{
  putc ('e', fp);
  putc ('s', fp);
  putc ('c', fp);
  putc ('E', fp);
  putc ('S', fp);
  putc ('C', fp);
  putc (':', fp);
}


static void
print_time (time_t t, FILE *fp)
{
  if (!t)
    ;
  else if ( t == (time_t)(-1) )
    putc ('?', fp);
  else
    fprintf (fp, "%lu", (unsigned long)t);
}


/* List one certificate in colon mode */
static void
list_cert_colon (KsbaCert cert, FILE *fp)
{
  int idx, trustletter = 0;
  unsigned char *p;

  fputs ("crt:", fp);
  trustletter = 0;
#if 0
  if (is_not_valid (cert))
    putc ('i', fp);
  else if ( is_revoked (cert) )
    putc ('r', fp);
  else if ( has_expired (cert))
    putcr ('e', fp);
  else
#endif
    {
      trustletter = '?'; /*get_validity_info ( pk, NULL );*/
      putc (trustletter, fp);
    }

  fprintf (fp, ":%u:%d::",
           /*keylen_of_cert (cert)*/1024,
           /* pubkey_algo_of_cert (cert)*/'R');

  /* we assume --fixed-list-mode for gpgsm */
  print_time ( ksba_cert_get_validity (cert, 0), fp);
  putc (':', fp);
  print_time ( ksba_cert_get_validity (cert, 1), fp);
  putc (':', fp);
  putc (':', fp);
  if ((p = ksba_cert_get_serial (cert)))
    {
      int i, len = (p[0] << 24) | (p[1] << 16) | (p[2] << 8) | p[3];
      for (i=0; i < len; i++)
        fprintf (fp,"%02X", p[4+i]);
      xfree (p);
    }
  putc (':', fp);
  putc (':', fp);
  if ((p = ksba_cert_get_issuer (cert,0)))
    {
      fputs (p, fp);  /* FIXME: Escape colons and linefeeds */
      xfree (p);
    }
  putc (':', fp);
  print_capabilities (cert, fp);
  putc ('\n', fp);

  p = gpgsm_get_fingerprint_hexstring (cert, GCRY_MD_SHA1);
  fprintf (fp, "fpr:::::::::%s:\n", p);
  xfree (p);
  if (opt.with_key_data)
    {
      if ( (p = gpgsm_get_keygrip_hexstring (cert)))
        {
          fprintf (fp, "grp:::::::::%s:\n", p);
          xfree (p);
        }
      print_key_data (cert, fp);
    }

  for (idx=0; (p = ksba_cert_get_subject (cert,idx)); idx++)
    {
      fprintf (fp, "uid:%c::::::::", trustletter);
      fputs (p, fp);  /* FIXME: Escape colons and linefeeds */
      xfree (p);
      putc (':', fp);
      putc (':', fp);
      putc ('\n', fp);
    }
}




/* List all keys or just the key given as NAMES */
void
gpgsm_list_keys (CTRL ctrl, STRLIST names, FILE *fp)
{
  KEYDB_HANDLE hd;
  KsbaCert cert = NULL;
  int rc=0;
  const char *lastresname, *resname;

  hd = keydb_new (0);
  if (!hd)
    rc = GNUPG_General_Error;
  else
    rc = keydb_search_first (hd);
  if (rc)
    {
      if (rc != -1)
        log_error ("keydb_search_first failed: %s\n", gnupg_strerror (rc) );
      goto leave;
    }

  lastresname = NULL;
  do
    {
      rc = keydb_get_cert (hd, &cert);
      if (rc) 
        {
          log_error ("keydb_get_cert failed: %s\n", gnupg_strerror (rc));
          goto leave;
        }
      
      resname = keydb_get_resource_name (hd);
      
      if (lastresname != resname ) 
        {
          int i;
          
          if (ctrl->no_server)
            {
              fprintf (fp, "%s\n", resname );
              for (i=strlen(resname); i; i-- )
                putchar('-');
              putc ('\n', fp);
              lastresname = resname;
            }
        }
      if (ctrl->with_colons)
        list_cert_colon (cert, fp);
      else
        list_cert_colon (cert, fp);
      ksba_cert_release (cert); 
      cert = NULL;
    }
  while (!(rc = keydb_search_next (hd)));
  if (rc && rc != -1)
    log_error ("keydb_search_next failed: %s\n", gnupg_strerror (rc));
  
 leave:
  ksba_cert_release (cert);
  keydb_release (hd);
}