~ubuntu-branches/ubuntu/precise/gnupg2/precise-updates

« back to all changes in this revision

Viewing changes to agent/call-scd.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
/* call-scd.c - fork of the scdaemon to do SC operations
 
2
 *      Copyright (C) 2001, 2002 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
/* Fixme: For now we have serialized all access to the scdaemon which
 
22
   make sense becuase the scdaemon can't handle concurrent connections
 
23
   right now.  We should however keep a list of connections and lock
 
24
   just that connection - it migth make sense to implemtn parts of
 
25
   this in Assuan.*/
 
26
 
 
27
#include <config.h>
 
28
#include <errno.h>
 
29
#include <stdio.h>
 
30
#include <stdlib.h>
 
31
#include <string.h>
 
32
#include <ctype.h>
 
33
#include <assert.h>
 
34
#include <unistd.h>
 
35
#include <sys/stat.h>
 
36
#include <sys/types.h>
 
37
#ifndef HAVE_W32_SYSTEM
 
38
#include <sys/wait.h>
 
39
#endif
 
40
#ifdef USE_GNU_PTH
 
41
# include <pth.h>
 
42
#endif
 
43
 
 
44
#include "agent.h"
 
45
#include <assuan.h>
 
46
 
 
47
#ifdef _POSIX_OPEN_MAX
 
48
#define MAX_OPEN_FDS _POSIX_OPEN_MAX
 
49
#else
 
50
#define MAX_OPEN_FDS 20
 
51
#endif
 
52
 
 
53
static ASSUAN_CONTEXT scd_ctx = NULL;
 
54
#ifdef USE_GNU_PTH
 
55
static pth_mutex_t scd_lock;
 
56
#endif
 
57
/* We need to keep track of the connection currently using the SCD.
 
58
   For a pipe server this is all a NOP because the connection will
 
59
   always have the connection indicator -1.  agent_reset_scd releases
 
60
   the active connection; i.e. sets it back to -1, so that a new
 
61
   connection can start using the SCD.  If we eventually allow
 
62
   multiple SCD session we will either make scdaemon multi-threaded or
 
63
   fork of a new scdaemon and let it see how it can get access to a
 
64
   reader. 
 
65
*/
 
66
static int active_connection_fd = -1;
 
67
static int active_connection = 0;
 
68
 
 
69
/* callback parameter for learn card */
 
70
struct learn_parm_s {
 
71
  void (*kpinfo_cb)(void*, const char *);
 
72
  void *kpinfo_cb_arg;
 
73
  void (*certinfo_cb)(void*, const char *);
 
74
  void *certinfo_cb_arg;
 
75
  void (*sinfo_cb)(void*, const char *, size_t, const char *);
 
76
  void *sinfo_cb_arg;
 
77
};
 
78
 
 
79
struct inq_needpin_s {
 
80
  ASSUAN_CONTEXT ctx;
 
81
  int (*getpin_cb)(void *, const char *, char*, size_t);
 
82
  void *getpin_cb_arg;
 
83
};
 
84
 
 
85
 
 
86
 
 
87
/* This function must be called once to initialize this module.  This
 
88
   has to be done before a second thread is spawned.  We can't do the
 
89
   static initialization because Pth emulation code might not be able
 
90
   to do a static init; in particualr, it is not possible for W32. */
 
91
void
 
92
initialize_module_call_scd (void)
 
93
{
 
94
#ifdef USE_GNU_PTH
 
95
  static int initialized;
 
96
 
 
97
  if (!initialized)
 
98
    if (pth_mutex_init (&scd_lock))
 
99
      initialized = 1;
 
100
#endif /*USE_GNU_PTH*/
 
101
}
 
102
 
 
103
 
 
104
static int 
 
105
unlock_scd (int rc)
 
106
{
 
107
#ifdef USE_GNU_PTH
 
108
  if (!pth_mutex_release (&scd_lock))
 
109
    {
 
110
      log_error ("failed to release the SCD lock\n");
 
111
      if (!rc)
 
112
        rc = gpg_error (GPG_ERR_INTERNAL);
 
113
    }
 
114
#endif /*USE_GNU_PTH*/
 
115
  return rc;
 
116
}
 
117
 
 
118
/* To make sure we leave no secrets in our image after forking of the
 
119
   scdaemon, we use this callback. */
 
120
static void
 
121
atfork_cb (void *opaque, int where)
 
122
{
 
123
  if (!where)
 
124
    gcry_control (GCRYCTL_TERM_SECMEM);
 
125
}
 
126
 
 
127
 
 
128
/* Fork off the SCdaemon if this has not already been done.  Note that
 
129
   this fucntion alos locks the daemon.  */
 
130
static int
 
131
start_scd (ctrl_t ctrl)
 
132
{
 
133
  int rc;
 
134
  const char *pgmname;
 
135
  ASSUAN_CONTEXT ctx;
 
136
  const char *argv[3];
 
137
  int no_close_list[3];
 
138
  int i;
 
139
 
 
140
#ifdef USE_GNU_PTH
 
141
  if (!pth_mutex_acquire (&scd_lock, 0, NULL))
 
142
    {
 
143
      log_error ("failed to acquire the SCD lock\n");
 
144
      return gpg_error (GPG_ERR_INTERNAL);
 
145
    }
 
146
#endif
 
147
 
 
148
  if (scd_ctx)
 
149
    {
 
150
      pid_t pid;
 
151
 
 
152
      /* If we are not the connection currently using the SCD, return
 
153
         an error. */
 
154
      if (!active_connection)
 
155
        {
 
156
          active_connection_fd = ctrl->connection_fd;
 
157
          active_connection = 1;
 
158
        }
 
159
      else if (ctrl->connection_fd != active_connection_fd)
 
160
        return unlock_scd (gpg_error (GPG_ERR_CONFLICT));
 
161
      
 
162
      /* Okay, we already started the scdaemon and it is used by us.*/
 
163
 
 
164
      /* We better do a sanity check now to see whether it has
 
165
         accidently died. */
 
166
#ifndef HAVE_W32_SYSTEM 
 
167
      pid = assuan_get_pid (scd_ctx);
 
168
      if (pid != (pid_t)(-1) && pid
 
169
          && ((rc=waitpid (pid, NULL, WNOHANG))==-1 || (rc == pid)) )
 
170
        {
 
171
          assuan_disconnect (scd_ctx);
 
172
          scd_ctx = NULL;
 
173
        }
 
174
      else
 
175
#endif
 
176
        return 0; 
 
177
    }
 
178
 
 
179
  if (opt.verbose)
 
180
    log_info ("no running SCdaemon - starting it\n");
 
181
      
 
182
  if (fflush (NULL))
 
183
    {
 
184
      gpg_error_t tmperr = gpg_error (gpg_err_code_from_errno (errno));
 
185
      log_error ("error flushing pending output: %s\n", strerror (errno));
 
186
      return unlock_scd (tmperr);
 
187
    }
 
188
 
 
189
  if (!opt.scdaemon_program || !*opt.scdaemon_program)
 
190
    opt.scdaemon_program = GNUPG_DEFAULT_SCDAEMON;
 
191
  if ( !(pgmname = strrchr (opt.scdaemon_program, '/')))
 
192
    pgmname = opt.scdaemon_program;
 
193
  else
 
194
    pgmname++;
 
195
 
 
196
  argv[0] = pgmname;
 
197
  argv[1] = "--server";
 
198
  argv[2] = NULL;
 
199
 
 
200
  i=0;
 
201
  if (!opt.running_detached)
 
202
    {
 
203
      if (log_get_fd () != -1)
 
204
        no_close_list[i++] = log_get_fd ();
 
205
      no_close_list[i++] = fileno (stderr);
 
206
    }
 
207
  no_close_list[i] = -1;
 
208
 
 
209
  /* Connect to the pinentry and perform initial handshaking */
 
210
  rc = assuan_pipe_connect2 (&ctx, opt.scdaemon_program, (char**)argv,
 
211
                             no_close_list, atfork_cb, NULL);
 
212
  if (rc)
 
213
    {
 
214
      log_error ("can't connect to the SCdaemon: %s\n",
 
215
                 assuan_strerror (rc));
 
216
      return unlock_scd (gpg_error (GPG_ERR_NO_SCDAEMON));
 
217
    }
 
218
  scd_ctx = ctx;
 
219
  active_connection_fd = ctrl->connection_fd;
 
220
  active_connection = 1;
 
221
 
 
222
  if (DBG_ASSUAN)
 
223
    log_debug ("connection to SCdaemon established\n");
 
224
 
 
225
  /* Tell the scdaemon that we want him to send us an event signal.
 
226
     But only do this if we are running as a regular sever and not
 
227
     simply as a pipe server. */
 
228
  if (ctrl->connection_fd != -1)
 
229
  {
 
230
#ifndef HAVE_W32_SYSTEM
 
231
    char buf[100];
 
232
 
 
233
    sprintf (buf, "OPTION event-signal=%d", SIGUSR2);
 
234
    assuan_transact (scd_ctx, buf, NULL, NULL, NULL, NULL, NULL, NULL);
 
235
#endif
 
236
  }
 
237
 
 
238
  return 0;
 
239
}
 
240
 
 
241
 
 
242
 
 
243
/* Reset the SCD if it has been used. */
 
244
int
 
245
agent_reset_scd (ctrl_t ctrl)
 
246
{
 
247
  int rc = 0;
 
248
 
 
249
#ifdef USE_GNU_PTH
 
250
  if (!pth_mutex_acquire (&scd_lock, 0, NULL))
 
251
    {
 
252
      log_error ("failed to acquire the SCD lock for reset\n");
 
253
      return gpg_error (GPG_ERR_INTERNAL);
 
254
    }
 
255
#endif
 
256
  if (active_connection && active_connection_fd == ctrl->connection_fd)
 
257
    {
 
258
      if (scd_ctx)
 
259
        rc = assuan_transact (scd_ctx, "RESET", NULL, NULL,
 
260
                              NULL, NULL, NULL, NULL);
 
261
      active_connection_fd = -1;
 
262
      active_connection = 0;
 
263
    }
 
264
 
 
265
  return unlock_scd (map_assuan_err (rc));
 
266
}
 
267
 
 
268
 
 
269
 
 
270
 
 
271
 
 
272
static AssuanError
 
273
learn_status_cb (void *opaque, const char *line)
 
274
{
 
275
  struct learn_parm_s *parm = opaque;
 
276
  const char *keyword = line;
 
277
  int keywordlen;
 
278
 
 
279
  for (keywordlen=0; *line && !spacep (line); line++, keywordlen++)
 
280
    ;
 
281
  while (spacep (line))
 
282
    line++;
 
283
  if (keywordlen == 8 && !memcmp (keyword, "CERTINFO", keywordlen))
 
284
    {
 
285
      parm->certinfo_cb (parm->certinfo_cb_arg, line);
 
286
    }
 
287
  else if (keywordlen == 11 && !memcmp (keyword, "KEYPAIRINFO", keywordlen))
 
288
    {
 
289
      parm->kpinfo_cb (parm->kpinfo_cb_arg, line);
 
290
    }
 
291
  else if (keywordlen && *line)
 
292
    {
 
293
      parm->sinfo_cb (parm->sinfo_cb_arg, keyword, keywordlen, line);
 
294
    }
 
295
  
 
296
  return 0;
 
297
}
 
298
 
 
299
/* Perform the learn command and return a list of all private keys
 
300
   stored on the card. */
 
301
int
 
302
agent_card_learn (ctrl_t ctrl,
 
303
                  void (*kpinfo_cb)(void*, const char *),
 
304
                  void *kpinfo_cb_arg,
 
305
                  void (*certinfo_cb)(void*, const char *),
 
306
                  void *certinfo_cb_arg,
 
307
                  void (*sinfo_cb)(void*, const char *, size_t, const char *),
 
308
                  void *sinfo_cb_arg)
 
309
{
 
310
  int rc;
 
311
  struct learn_parm_s parm;
 
312
 
 
313
  rc = start_scd (ctrl);
 
314
  if (rc)
 
315
    return rc;
 
316
 
 
317
  memset (&parm, 0, sizeof parm);
 
318
  parm.kpinfo_cb = kpinfo_cb;
 
319
  parm.kpinfo_cb_arg = kpinfo_cb_arg;
 
320
  parm.certinfo_cb = certinfo_cb;
 
321
  parm.certinfo_cb_arg = certinfo_cb_arg;
 
322
  parm.sinfo_cb = sinfo_cb;
 
323
  parm.sinfo_cb_arg = sinfo_cb_arg;
 
324
  rc = assuan_transact (scd_ctx, "LEARN --force",
 
325
                        NULL, NULL, NULL, NULL,
 
326
                        learn_status_cb, &parm);
 
327
  if (rc)
 
328
    return unlock_scd (map_assuan_err (rc));
 
329
 
 
330
  return unlock_scd (0);
 
331
}
 
332
 
 
333
 
 
334
 
 
335
static AssuanError
 
336
get_serialno_cb (void *opaque, const char *line)
 
337
{
 
338
  char **serialno = opaque;
 
339
  const char *keyword = line;
 
340
  const char *s;
 
341
  int keywordlen, n;
 
342
 
 
343
  for (keywordlen=0; *line && !spacep (line); line++, keywordlen++)
 
344
    ;
 
345
  while (spacep (line))
 
346
    line++;
 
347
 
 
348
  if (keywordlen == 8 && !memcmp (keyword, "SERIALNO", keywordlen))
 
349
    {
 
350
      if (*serialno)
 
351
        return ASSUAN_Unexpected_Status;
 
352
      for (n=0,s=line; hexdigitp (s); s++, n++)
 
353
        ;
 
354
      if (!n || (n&1)|| !(spacep (s) || !*s) )
 
355
        return ASSUAN_Invalid_Status;
 
356
      *serialno = xtrymalloc (n+1);
 
357
      if (!*serialno)
 
358
        return ASSUAN_Out_Of_Core;
 
359
      memcpy (*serialno, line, n);
 
360
      (*serialno)[n] = 0;
 
361
    }
 
362
  
 
363
  return 0;
 
364
}
 
365
 
 
366
/* Return the serial number of the card or an appropriate error.  The
 
367
   serial number is returned as a hexstring. */
 
368
int
 
369
agent_card_serialno (ctrl_t ctrl, char **r_serialno)
 
370
{
 
371
  int rc;
 
372
  char *serialno = NULL;
 
373
 
 
374
  rc = start_scd (ctrl);
 
375
  if (rc)
 
376
    return rc;
 
377
 
 
378
  /* Hmm, do we really need this reset - scddaemon should do this or
 
379
     we can do this if we for some reason figure out that the
 
380
     operation might have failed due to a missing RESET.  Hmmm, I feel
 
381
     this is really SCdaemon's duty */
 
382
/*    rc = assuan_transact (scd_ctx, "RESET", NULL, NULL, NULL, NULL, NULL, NULL); */
 
383
/*    if (rc) */
 
384
/*      return unlock_scd (map_assuan_err (rc)); */
 
385
 
 
386
  rc = assuan_transact (scd_ctx, "SERIALNO",
 
387
                        NULL, NULL, NULL, NULL,
 
388
                        get_serialno_cb, &serialno);
 
389
  if (rc)
 
390
    {
 
391
      xfree (serialno);
 
392
      return unlock_scd (map_assuan_err (rc));
 
393
    }
 
394
  *r_serialno = serialno;
 
395
  return unlock_scd (0);
 
396
}
 
397
 
 
398
 
 
399
static AssuanError
 
400
membuf_data_cb (void *opaque, const void *buffer, size_t length)
 
401
{
 
402
  membuf_t *data = opaque;
 
403
 
 
404
  if (buffer)
 
405
    put_membuf (data, buffer, length);
 
406
  return 0;
 
407
}
 
408
  
 
409
/* Handle the NEEDPIN inquiry. */
 
410
static AssuanError
 
411
inq_needpin (void *opaque, const char *line)
 
412
{
 
413
  struct inq_needpin_s *parm = opaque;
 
414
  char *pin;
 
415
  size_t pinlen;
 
416
  int rc;
 
417
 
 
418
  if (!(!strncmp (line, "NEEDPIN", 7) && (line[7] == ' ' || !line[7])))
 
419
    {
 
420
      log_error ("unsupported inquiry `%s'\n", line);
 
421
      return ASSUAN_Inquire_Unknown;
 
422
    }
 
423
  line += 7;
 
424
 
 
425
  pinlen = 90;
 
426
  pin = gcry_malloc_secure (pinlen);
 
427
  if (!pin)
 
428
    return ASSUAN_Out_Of_Core;
 
429
 
 
430
  rc = parm->getpin_cb (parm->getpin_cb_arg, line, pin, pinlen);
 
431
  if (rc)
 
432
    rc = ASSUAN_Canceled;
 
433
  if (!rc)
 
434
    rc = assuan_send_data (parm->ctx, pin, pinlen);
 
435
  xfree (pin);
 
436
 
 
437
  return rc;
 
438
}
 
439
 
 
440
 
 
441
 
 
442
/* Create a signature using the current card */
 
443
int
 
444
agent_card_pksign (ctrl_t ctrl,
 
445
                   const char *keyid,
 
446
                   int (*getpin_cb)(void *, const char *, char*, size_t),
 
447
                   void *getpin_cb_arg,
 
448
                   const unsigned char *indata, size_t indatalen,
 
449
                   char **r_buf, size_t *r_buflen)
 
450
{
 
451
  int rc, i;
 
452
  char *p, line[ASSUAN_LINELENGTH];
 
453
  membuf_t data;
 
454
  struct inq_needpin_s inqparm;
 
455
  size_t len;
 
456
  unsigned char *sigbuf;
 
457
  size_t sigbuflen;
 
458
 
 
459
  *r_buf = NULL;
 
460
  rc = start_scd (ctrl);
 
461
  if (rc)
 
462
    return rc;
 
463
 
 
464
  if (indatalen*2 + 50 > DIM(line))
 
465
    return unlock_scd (gpg_error (GPG_ERR_GENERAL));
 
466
 
 
467
  sprintf (line, "SETDATA ");
 
468
  p = line + strlen (line);
 
469
  for (i=0; i < indatalen ; i++, p += 2 )
 
470
    sprintf (p, "%02X", indata[i]);
 
471
  rc = assuan_transact (scd_ctx, line, NULL, NULL, NULL, NULL, NULL, NULL);
 
472
  if (rc)
 
473
    return unlock_scd (map_assuan_err (rc));
 
474
 
 
475
  init_membuf (&data, 1024);
 
476
  inqparm.ctx = scd_ctx;
 
477
  inqparm.getpin_cb = getpin_cb;
 
478
  inqparm.getpin_cb_arg = getpin_cb_arg;
 
479
  snprintf (line, DIM(line)-1, "PKSIGN %s", keyid);
 
480
  line[DIM(line)-1] = 0;
 
481
  rc = assuan_transact (scd_ctx, line,
 
482
                        membuf_data_cb, &data,
 
483
                        inq_needpin, &inqparm,
 
484
                        NULL, NULL);
 
485
  if (rc)
 
486
    {
 
487
      xfree (get_membuf (&data, &len));
 
488
      return unlock_scd (map_assuan_err (rc));
 
489
    }
 
490
  sigbuf = get_membuf (&data, &sigbuflen);
 
491
 
 
492
  /* create an S-expression from it which is formatted like this:
 
493
     "(7:sig-val(3:rsa(1:sSIGBUFLEN:SIGBUF)))" */
 
494
  *r_buflen = 21 + 11 + sigbuflen + 4;
 
495
  *r_buf = xtrymalloc (*r_buflen);
 
496
  if (!*r_buf)
 
497
    {
 
498
      gpg_error_t tmperr = out_of_core ();
 
499
      xfree (*r_buf);
 
500
      return unlock_scd (tmperr);
 
501
    }
 
502
  p = stpcpy (*r_buf, "(7:sig-val(3:rsa(1:s" );
 
503
  sprintf (p, "%u:", (unsigned int)sigbuflen);
 
504
  p += strlen (p);
 
505
  memcpy (p, sigbuf, sigbuflen);
 
506
  p += sigbuflen;
 
507
  strcpy (p, ")))");
 
508
  xfree (sigbuf);
 
509
 
 
510
  assert (gcry_sexp_canon_len (*r_buf, *r_buflen, NULL, NULL));
 
511
  return unlock_scd (0);
 
512
}
 
513
 
 
514
/* Decipher INDATA using the current card. Note that the returned value is */
 
515
int
 
516
agent_card_pkdecrypt (ctrl_t ctrl,
 
517
                      const char *keyid,
 
518
                      int (*getpin_cb)(void *, const char *, char*, size_t),
 
519
                      void *getpin_cb_arg,
 
520
                      const unsigned char *indata, size_t indatalen,
 
521
                      char **r_buf, size_t *r_buflen)
 
522
{
 
523
  int rc, i;
 
524
  char *p, line[ASSUAN_LINELENGTH];
 
525
  membuf_t data;
 
526
  struct inq_needpin_s inqparm;
 
527
  size_t len;
 
528
 
 
529
  *r_buf = NULL;
 
530
  rc = start_scd (ctrl);
 
531
  if (rc)
 
532
    return rc;
 
533
 
 
534
  /* FIXME: use secure memory where appropriate */
 
535
  if (indatalen*2 + 50 > DIM(line))
 
536
    return unlock_scd (gpg_error (GPG_ERR_GENERAL));
 
537
 
 
538
  sprintf (line, "SETDATA ");
 
539
  p = line + strlen (line);
 
540
  for (i=0; i < indatalen ; i++, p += 2 )
 
541
    sprintf (p, "%02X", indata[i]);
 
542
  rc = assuan_transact (scd_ctx, line, NULL, NULL, NULL, NULL, NULL, NULL);
 
543
  if (rc)
 
544
    return unlock_scd (map_assuan_err (rc));
 
545
 
 
546
  init_membuf (&data, 1024);
 
547
  inqparm.ctx = scd_ctx;
 
548
  inqparm.getpin_cb = getpin_cb;
 
549
  inqparm.getpin_cb_arg = getpin_cb_arg;
 
550
  snprintf (line, DIM(line)-1, "PKDECRYPT %s", keyid);
 
551
  line[DIM(line)-1] = 0;
 
552
  rc = assuan_transact (scd_ctx, line,
 
553
                        membuf_data_cb, &data,
 
554
                        inq_needpin, &inqparm,
 
555
                        NULL, NULL);
 
556
  if (rc)
 
557
    {
 
558
      xfree (get_membuf (&data, &len));
 
559
      return unlock_scd (map_assuan_err (rc));
 
560
    }
 
561
  *r_buf = get_membuf (&data, r_buflen);
 
562
  if (!*r_buf)
 
563
    return unlock_scd (gpg_error (GPG_ERR_ENOMEM));
 
564
 
 
565
  return unlock_scd (0);
 
566
}
 
567
 
 
568
 
 
569
 
 
570
/* Read a certificate with ID into R_BUF and R_BUFLEN. */
 
571
int
 
572
agent_card_readcert (ctrl_t ctrl,
 
573
                     const char *id, char **r_buf, size_t *r_buflen)
 
574
{
 
575
  int rc;
 
576
  char line[ASSUAN_LINELENGTH];
 
577
  membuf_t data;
 
578
  size_t len;
 
579
 
 
580
  *r_buf = NULL;
 
581
  rc = start_scd (ctrl);
 
582
  if (rc)
 
583
    return rc;
 
584
 
 
585
  init_membuf (&data, 1024);
 
586
  snprintf (line, DIM(line)-1, "READCERT %s", id);
 
587
  line[DIM(line)-1] = 0;
 
588
  rc = assuan_transact (scd_ctx, line,
 
589
                        membuf_data_cb, &data,
 
590
                        NULL, NULL,
 
591
                        NULL, NULL);
 
592
  if (rc)
 
593
    {
 
594
      xfree (get_membuf (&data, &len));
 
595
      return unlock_scd (map_assuan_err (rc));
 
596
    }
 
597
  *r_buf = get_membuf (&data, r_buflen);
 
598
  if (!*r_buf)
 
599
    return unlock_scd (gpg_error (GPG_ERR_ENOMEM));
 
600
 
 
601
  return unlock_scd (0);
 
602
}
 
603
 
 
604
 
 
605
 
 
606
/* Read a key with ID and return it in an allocate buffer pointed to
 
607
   by r_BUF as a valid S-expression. */
 
608
int
 
609
agent_card_readkey (ctrl_t ctrl, const char *id, unsigned char **r_buf)
 
610
{
 
611
  int rc;
 
612
  char line[ASSUAN_LINELENGTH];
 
613
  membuf_t data;
 
614
  size_t len, buflen;
 
615
 
 
616
  *r_buf = NULL;
 
617
  rc = start_scd (ctrl);
 
618
  if (rc)
 
619
    return rc;
 
620
 
 
621
  init_membuf (&data, 1024);
 
622
  snprintf (line, DIM(line)-1, "READKEY %s", id);
 
623
  line[DIM(line)-1] = 0;
 
624
  rc = assuan_transact (scd_ctx, line,
 
625
                        membuf_data_cb, &data,
 
626
                        NULL, NULL,
 
627
                        NULL, NULL);
 
628
  if (rc)
 
629
    {
 
630
      xfree (get_membuf (&data, &len));
 
631
      return unlock_scd (map_assuan_err (rc));
 
632
    }
 
633
  *r_buf = get_membuf (&data, &buflen);
 
634
  if (!*r_buf)
 
635
    return unlock_scd (gpg_error (GPG_ERR_ENOMEM));
 
636
 
 
637
  if (!gcry_sexp_canon_len (*r_buf, buflen, NULL, NULL))
 
638
    {
 
639
      xfree (*r_buf); *r_buf = NULL;
 
640
      return unlock_scd (gpg_error (GPG_ERR_INV_VALUE));
 
641
    }
 
642
 
 
643
  return unlock_scd (0);
 
644
}
 
645
 
 
646
 
 
647
 
 
648
 
 
649
static AssuanError
 
650
pass_status_thru (void *opaque, const char *line)
 
651
{
 
652
  ASSUAN_CONTEXT ctx = opaque;
 
653
  char keyword[200];
 
654
  int i;
 
655
 
 
656
  for (i=0; *line && !spacep (line) && i < DIM(keyword)-1; line++, i++)
 
657
    keyword[i] = *line;
 
658
  keyword[i] = 0;
 
659
  /* truncate any remaining keyword stuff. */
 
660
  for (; *line && !spacep (line); line++)
 
661
    ;
 
662
  while (spacep (line))
 
663
    line++;
 
664
 
 
665
  assuan_write_status (ctx, keyword, line);
 
666
  return 0;
 
667
}
 
668
 
 
669
static AssuanError
 
670
pass_data_thru (void *opaque, const void *buffer, size_t length)
 
671
{
 
672
  ASSUAN_CONTEXT ctx = opaque;
 
673
 
 
674
  assuan_send_data (ctx, buffer, length);
 
675
  return 0;
 
676
}
 
677
 
 
678
 
 
679
/* Send the line CMDLINE with command for the SCDdaemon to it and send
 
680
   all status messages back.  This command is used as a general quoting
 
681
   mechanism to pass everything verbatim to SCDAEMOPN.  The PIN
 
682
   inquirey is handled inside gpg-agent. */
 
683
int
 
684
agent_card_scd (ctrl_t ctrl, const char *cmdline,
 
685
                int (*getpin_cb)(void *, const char *, char*, size_t),
 
686
                void *getpin_cb_arg, void *assuan_context)
 
687
{
 
688
  int rc;
 
689
  struct inq_needpin_s inqparm;
 
690
 
 
691
  rc = start_scd (ctrl);
 
692
  if (rc)
 
693
    return rc;
 
694
 
 
695
  inqparm.ctx = scd_ctx;
 
696
  inqparm.getpin_cb = getpin_cb;
 
697
  inqparm.getpin_cb_arg = getpin_cb_arg;
 
698
  rc = assuan_transact (scd_ctx, cmdline,
 
699
                        pass_data_thru, assuan_context,
 
700
                        inq_needpin, &inqparm,
 
701
                        pass_status_thru, assuan_context);
 
702
  if (rc)
 
703
    {
 
704
      return unlock_scd (map_assuan_err (rc));
 
705
    }
 
706
 
 
707
  return unlock_scd (0);
 
708
}
 
709
 
 
710