mount does now work in server and standalone mode.
[gnupg.git] / agent / call-scd.c
blob52108684850c60aff794f10db807ef1a7965bc6b
1 /* call-scd.c - fork of the scdaemon to do SC operations
2 * Copyright (C) 2001, 2002, 2005, 2007 Free Software Foundation, Inc.
4 * This file is part of GnuPG.
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 3 of the License, or
9 * (at your option) any later version.
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.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, see <http://www.gnu.org/licenses/>.
20 #include <config.h>
21 #include <errno.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <ctype.h>
26 #include <assert.h>
27 #include <unistd.h>
28 #include <signal.h>
29 #include <sys/stat.h>
30 #include <sys/types.h>
31 #ifndef HAVE_W32_SYSTEM
32 #include <sys/wait.h>
33 #endif
34 #include <pth.h>
36 #include "agent.h"
37 #include <assuan.h>
39 #ifdef _POSIX_OPEN_MAX
40 #define MAX_OPEN_FDS _POSIX_OPEN_MAX
41 #else
42 #define MAX_OPEN_FDS 20
43 #endif
45 /* Definition of module local data of the CTRL structure. */
46 struct scd_local_s
48 /* We keep a list of all allocated context with a an achnor at
49 SCD_LOCAL_LIST (see below). */
50 struct scd_local_s *next_local;
52 /* We need to get back to the ctrl object actually referencing this
53 structure. This is really an awkward way of enumerint the lcoal
54 contects. A much cleaner way would be to keep a global list of
55 ctrl objects to enumerate them. */
56 ctrl_t ctrl_backlink;
58 assuan_context_t ctx; /* NULL or session context for the SCdaemon
59 used with this connection. */
60 int locked; /* This flag is used to assert proper use of
61 start_scd and unlock_scd. */
66 /* Callback parameter for learn card */
67 struct learn_parm_s
69 void (*kpinfo_cb)(void*, const char *);
70 void *kpinfo_cb_arg;
71 void (*certinfo_cb)(void*, const char *);
72 void *certinfo_cb_arg;
73 void (*sinfo_cb)(void*, const char *, size_t, const char *);
74 void *sinfo_cb_arg;
77 struct inq_needpin_s
79 assuan_context_t ctx;
80 int (*getpin_cb)(void *, const char *, char*, size_t);
81 void *getpin_cb_arg;
82 assuan_context_t passthru; /* If not NULL, pass unknown inquiries
83 up to the caller. */
87 /* To keep track of all active SCD contexts, we keep a linked list
88 anchored at this variable. */
89 static struct scd_local_s *scd_local_list;
91 /* A Mutex used inside the start_scd function. */
92 static pth_mutex_t start_scd_lock;
94 /* A malloced string with the name of the socket to be used for
95 additional connections. May be NULL if not provided by
96 SCdaemon. */
97 static char *socket_name;
99 /* The context of the primary connection. This is also used as a flag
100 to indicate whether the scdaemon has been started. */
101 static assuan_context_t primary_scd_ctx;
103 /* To allow reuse of the primary connection, the following flag is set
104 to true if the primary context has been reset and is not in use by
105 any connection. */
106 static int primary_scd_ctx_reusable;
110 /* Local prototypes. */
111 static gpg_error_t membuf_data_cb (void *opaque,
112 const void *buffer, size_t length);
117 /* This function must be called once to initialize this module. This
118 has to be done before a second thread is spawned. We can't do the
119 static initialization because Pth emulation code might not be able
120 to do a static init; in particular, it is not possible for W32. */
121 void
122 initialize_module_call_scd (void)
124 static int initialized;
126 if (!initialized)
128 if (!pth_mutex_init (&start_scd_lock))
129 log_fatal ("error initializing mutex: %s\n", strerror (errno));
130 initialized = 1;
135 static void
136 dump_mutex_state (pth_mutex_t *m)
138 #ifdef _W32_PTH_H
139 (void)m;
140 log_printf ("unknown under W32");
141 #else
142 if (!(m->mx_state & PTH_MUTEX_INITIALIZED))
143 log_printf ("not_initialized");
144 else if (!(m->mx_state & PTH_MUTEX_LOCKED))
145 log_printf ("not_locked");
146 else
147 log_printf ("locked tid=0x%lx count=%lu", (long)m->mx_owner, m->mx_count);
148 #endif
152 /* This function may be called to print infromation pertaining to the
153 current state of this module to the log. */
154 void
155 agent_scd_dump_state (void)
157 log_info ("agent_scd_dump_state: scd_lock=");
158 dump_mutex_state (&start_scd_lock);
159 log_printf ("\n");
160 log_info ("agent_scd_dump_state: primary_scd_ctx=%p pid=%ld reusable=%d\n",
161 primary_scd_ctx,
162 (long)assuan_get_pid (primary_scd_ctx),
163 primary_scd_ctx_reusable);
164 if (socket_name)
165 log_info ("agent_scd_dump_state: socket=`%s'\n", socket_name);
169 /* The unlock_scd function shall be called after having accessed the
170 SCD. It is currently not very useful but gives an opportunity to
171 keep track of connections currently calling SCD. Note that the
172 "lock" operation is done by the start_scd() function which must be
173 called and error checked before any SCD operation. CTRL is the
174 usual connection context and RC the error code to be passed trhough
175 the function. */
176 static int
177 unlock_scd (ctrl_t ctrl, int rc)
179 if (ctrl->scd_local->locked != 1)
181 log_error ("unlock_scd: invalid lock count (%d)\n",
182 ctrl->scd_local->locked);
183 if (!rc)
184 rc = gpg_error (GPG_ERR_INTERNAL);
186 ctrl->scd_local->locked = 0;
187 return rc;
190 /* To make sure we leave no secrets in our image after forking of the
191 scdaemon, we use this callback. */
192 static void
193 atfork_cb (void *opaque, int where)
195 (void)opaque;
197 if (!where)
198 gcry_control (GCRYCTL_TERM_SECMEM);
202 /* Fork off the SCdaemon if this has not already been done. Lock the
203 daemon and make sure that a proper context has been setup in CTRL.
204 This function might also lock the daemon, which means that the
205 caller must call unlock_scd after this fucntion has returned
206 success and the actual Assuan transaction been done. */
207 static int
208 start_scd (ctrl_t ctrl)
210 gpg_error_t err = 0;
211 const char *pgmname;
212 assuan_context_t ctx = NULL;
213 const char *argv[3];
214 int no_close_list[3];
215 int i;
216 int rc;
218 if (opt.disable_scdaemon)
219 return gpg_error (GPG_ERR_NOT_SUPPORTED);
221 /* If this is the first call for this session, setup the local data
222 structure. */
223 if (!ctrl->scd_local)
225 ctrl->scd_local = xtrycalloc (1, sizeof *ctrl->scd_local);
226 if (!ctrl->scd_local)
227 return gpg_error_from_syserror ();
228 ctrl->scd_local->ctrl_backlink = ctrl;
229 ctrl->scd_local->next_local = scd_local_list;
230 scd_local_list = ctrl->scd_local;
234 /* Assert that the lock count is as expected. */
235 if (ctrl->scd_local->locked)
237 log_error ("start_scd: invalid lock count (%d)\n",
238 ctrl->scd_local->locked);
239 return gpg_error (GPG_ERR_INTERNAL);
241 ctrl->scd_local->locked++;
243 if (ctrl->scd_local->ctx)
244 return 0; /* Okay, the context is fine. We used to test for an
245 alive context here and do an disconnect. Now that we
246 have a ticker function to check for it, it is easier
247 not to check here but to let the connection run on an
248 error instead. */
251 /* We need to protect the following code. */
252 if (!pth_mutex_acquire (&start_scd_lock, 0, NULL))
254 log_error ("failed to acquire the start_scd lock: %s\n",
255 strerror (errno));
256 return gpg_error (GPG_ERR_INTERNAL);
259 /* Check whether the pipe server has already been started and in
260 this case either reuse a lingering pipe connection or establish a
261 new socket based one. */
262 if (primary_scd_ctx && primary_scd_ctx_reusable)
264 ctx = primary_scd_ctx;
265 primary_scd_ctx_reusable = 0;
266 if (opt.verbose)
267 log_info ("new connection to SCdaemon established (reusing)\n");
268 goto leave;
271 rc = assuan_new (&ctx);
272 if (rc)
274 log_error ("can't allocate assuan context: %s\n", gpg_strerror (rc));
275 err = rc;
276 goto leave;
279 if (socket_name)
281 rc = assuan_socket_connect (ctx, socket_name, 0);
282 if (rc)
284 log_error ("can't connect to socket `%s': %s\n",
285 socket_name, gpg_strerror (rc));
286 err = gpg_error (GPG_ERR_NO_SCDAEMON);
287 goto leave;
290 if (opt.verbose)
291 log_info ("new connection to SCdaemon established\n");
292 goto leave;
295 if (primary_scd_ctx)
297 log_info ("SCdaemon is running but won't accept further connections\n");
298 err = gpg_error (GPG_ERR_NO_SCDAEMON);
299 goto leave;
302 /* Nope, it has not been started. Fire it up now. */
303 if (opt.verbose)
304 log_info ("no running SCdaemon - starting it\n");
306 if (fflush (NULL))
308 #ifndef HAVE_W32_SYSTEM
309 err = gpg_error_from_syserror ();
310 #endif
311 log_error ("error flushing pending output: %s\n", strerror (errno));
312 /* At least Windows XP fails here with EBADF. According to docs
313 and Wine an fflush(NULL) is the same as _flushall. However
314 the Wime implementaion does not flush stdin,stdout and stderr
315 - see above. Lets try to ignore the error. */
316 #ifndef HAVE_W32_SYSTEM
317 goto leave;
318 #endif
321 if (!opt.scdaemon_program || !*opt.scdaemon_program)
322 opt.scdaemon_program = gnupg_module_name (GNUPG_MODULE_NAME_SCDAEMON);
323 if ( !(pgmname = strrchr (opt.scdaemon_program, '/')))
324 pgmname = opt.scdaemon_program;
325 else
326 pgmname++;
328 argv[0] = pgmname;
329 argv[1] = "--multi-server";
330 argv[2] = NULL;
332 i=0;
333 if (!opt.running_detached)
335 if (log_get_fd () != -1)
336 no_close_list[i++] = log_get_fd ();
337 no_close_list[i++] = fileno (stderr);
339 no_close_list[i] = -1;
341 /* Connect to the pinentry and perform initial handshaking. Use
342 detached flag (128) so that under W32 SCDAEMON does not show up a
343 new window. */
344 rc = assuan_pipe_connect_ext (ctx, opt.scdaemon_program, argv,
345 no_close_list, atfork_cb, NULL, 128);
346 if (rc)
348 log_error ("can't connect to the SCdaemon: %s\n",
349 gpg_strerror (rc));
350 err = gpg_error (GPG_ERR_NO_SCDAEMON);
351 goto leave;
354 if (opt.verbose)
355 log_debug ("first connection to SCdaemon established\n");
357 if (DBG_ASSUAN)
358 assuan_set_log_stream (ctx, log_get_stream ());
360 /* Get the name of the additional socket opened by scdaemon. */
362 membuf_t data;
363 unsigned char *databuf;
364 size_t datalen;
366 xfree (socket_name);
367 socket_name = NULL;
368 init_membuf (&data, 256);
369 assuan_transact (ctx, "GETINFO socket_name",
370 membuf_data_cb, &data, NULL, NULL, NULL, NULL);
372 databuf = get_membuf (&data, &datalen);
373 if (databuf && datalen)
375 socket_name = xtrymalloc (datalen + 1);
376 if (!socket_name)
377 log_error ("warning: can't store socket name: %s\n",
378 strerror (errno));
379 else
381 memcpy (socket_name, databuf, datalen);
382 socket_name[datalen] = 0;
383 if (DBG_ASSUAN)
384 log_debug ("additional connections at `%s'\n", socket_name);
387 xfree (databuf);
390 /* Tell the scdaemon we want him to send us an event signal. */
392 char buf[100];
394 #ifdef HAVE_W32_SYSTEM
395 snprintf (buf, sizeof buf, "OPTION event-signal=%lx",
396 (unsigned long)get_agent_scd_notify_event ());
397 #else
398 snprintf (buf, sizeof buf, "OPTION event-signal=%d", SIGUSR2);
399 #endif
400 assuan_transact (ctx, buf, NULL, NULL, NULL, NULL, NULL, NULL);
403 primary_scd_ctx = ctx;
404 primary_scd_ctx_reusable = 0;
406 leave:
407 if (err)
409 unlock_scd (ctrl, err);
410 if (ctx)
411 assuan_release (ctx);
413 else
415 ctrl->scd_local->ctx = ctx;
417 if (!pth_mutex_release (&start_scd_lock))
418 log_error ("failed to release the start_scd lock: %s\n", strerror (errno));
419 return err;
423 /* Check whether the SCdaemon is active. This is a fast check without
424 any locking and might give a wrong result if another thread is about
425 to start the daemon or the daemon is about to be stopped.. */
427 agent_scd_check_running (void)
429 return !!primary_scd_ctx;
433 /* Check whether the Scdaemon is still alive and clean it up if not. */
434 void
435 agent_scd_check_aliveness (void)
437 pth_event_t evt;
438 pid_t pid;
439 #ifdef HAVE_W32_SYSTEM
440 DWORD rc;
441 #else
442 int rc;
443 #endif
445 if (!primary_scd_ctx)
446 return; /* No scdaemon running. */
448 /* This is not a critical function so we use a short timeout while
449 acquiring the lock. */
450 evt = pth_event (PTH_EVENT_TIME, pth_timeout (1, 0));
451 if (!pth_mutex_acquire (&start_scd_lock, 0, evt))
453 if (pth_event_occurred (evt))
455 if (opt.verbose > 1)
456 log_info ("failed to acquire the start_scd lock while"
457 " doing an aliveness check: %s\n", "timeout");
459 else
460 log_error ("failed to acquire the start_scd lock while"
461 " doing an aliveness check: %s\n", strerror (errno));
462 pth_event_free (evt, PTH_FREE_THIS);
463 return;
465 pth_event_free (evt, PTH_FREE_THIS);
467 if (primary_scd_ctx)
469 pid = assuan_get_pid (primary_scd_ctx);
470 #ifdef HAVE_W32_SYSTEM
471 /* If we have a PID we disconnect if either GetExitProcessCode
472 fails or if ir returns the exit code of the scdaemon. 259 is
473 the error code for STILL_ALIVE. */
474 if (pid != (pid_t)(void*)(-1) && pid
475 && (!GetExitCodeProcess ((HANDLE)pid, &rc) || rc != 259))
476 #else
477 if (pid != (pid_t)(-1) && pid
478 && ((rc=waitpid (pid, NULL, WNOHANG))==-1 || (rc == pid)) )
479 #endif
481 /* Okay, scdaemon died. Disconnect the primary connection
482 now but take care that it won't do another wait. Also
483 cleanup all other connections and release their
484 resources. The next use will start a new daemon then.
485 Due to the use of the START_SCD_LOCAL we are sure that
486 none of these context are actually in use. */
487 struct scd_local_s *sl;
489 assuan_set_flag (primary_scd_ctx, ASSUAN_NO_WAITPID, 1);
490 assuan_release (primary_scd_ctx);
492 for (sl=scd_local_list; sl; sl = sl->next_local)
494 if (sl->ctx)
496 if (sl->ctx != primary_scd_ctx)
497 assuan_release (sl->ctx);
498 sl->ctx = NULL;
502 primary_scd_ctx = NULL;
503 primary_scd_ctx_reusable = 0;
505 xfree (socket_name);
506 socket_name = NULL;
510 if (!pth_mutex_release (&start_scd_lock))
511 log_error ("failed to release the start_scd lock while"
512 " doing the aliveness check: %s\n", strerror (errno));
517 /* Reset the SCD if it has been used. Actually it is not a reset but
518 a cleanup of resources used by the current connection. */
520 agent_reset_scd (ctrl_t ctrl)
522 if (ctrl->scd_local)
524 if (ctrl->scd_local->ctx)
526 /* We can't disconnect the primary context because libassuan
527 does a waitpid on it and thus the system would hang.
528 Instead we send a reset and keep that connection for
529 reuse. */
530 if (ctrl->scd_local->ctx == primary_scd_ctx)
532 /* Send a RESTART to the SCD. This is required for the
533 primary connection as a kind of virtual EOF; we don't
534 have another way to tell it that the next command
535 should be viewed as if a new connection has been
536 made. For the non-primary connections this is not
537 needed as we simply close the socket. We don't check
538 for an error here because the RESTART may fail for
539 example if the scdaemon has already been terminated.
540 Anyway, we need to set the reusable flag to make sure
541 that the aliveness check can clean it up. */
542 assuan_transact (primary_scd_ctx, "RESTART",
543 NULL, NULL, NULL, NULL, NULL, NULL);
544 primary_scd_ctx_reusable = 1;
546 else
547 assuan_release (ctrl->scd_local->ctx);
548 ctrl->scd_local->ctx = NULL;
551 /* Remove the local context from our list and release it. */
552 if (!scd_local_list)
553 BUG ();
554 else if (scd_local_list == ctrl->scd_local)
555 scd_local_list = ctrl->scd_local->next_local;
556 else
558 struct scd_local_s *sl;
560 for (sl=scd_local_list; sl->next_local; sl = sl->next_local)
561 if (sl->next_local == ctrl->scd_local)
562 break;
563 if (!sl->next_local)
564 BUG ();
565 sl->next_local = ctrl->scd_local->next_local;
567 xfree (ctrl->scd_local);
568 ctrl->scd_local = NULL;
571 return 0;
576 static gpg_error_t
577 learn_status_cb (void *opaque, const char *line)
579 struct learn_parm_s *parm = opaque;
580 const char *keyword = line;
581 int keywordlen;
583 for (keywordlen=0; *line && !spacep (line); line++, keywordlen++)
585 while (spacep (line))
586 line++;
587 if (keywordlen == 8 && !memcmp (keyword, "CERTINFO", keywordlen))
589 parm->certinfo_cb (parm->certinfo_cb_arg, line);
591 else if (keywordlen == 11 && !memcmp (keyword, "KEYPAIRINFO", keywordlen))
593 parm->kpinfo_cb (parm->kpinfo_cb_arg, line);
595 else if (keywordlen && *line)
597 parm->sinfo_cb (parm->sinfo_cb_arg, keyword, keywordlen, line);
600 return 0;
603 /* Perform the LEARN command and return a list of all private keys
604 stored on the card. */
606 agent_card_learn (ctrl_t ctrl,
607 void (*kpinfo_cb)(void*, const char *),
608 void *kpinfo_cb_arg,
609 void (*certinfo_cb)(void*, const char *),
610 void *certinfo_cb_arg,
611 void (*sinfo_cb)(void*, const char *, size_t, const char *),
612 void *sinfo_cb_arg)
614 int rc;
615 struct learn_parm_s parm;
617 rc = start_scd (ctrl);
618 if (rc)
619 return rc;
621 memset (&parm, 0, sizeof parm);
622 parm.kpinfo_cb = kpinfo_cb;
623 parm.kpinfo_cb_arg = kpinfo_cb_arg;
624 parm.certinfo_cb = certinfo_cb;
625 parm.certinfo_cb_arg = certinfo_cb_arg;
626 parm.sinfo_cb = sinfo_cb;
627 parm.sinfo_cb_arg = sinfo_cb_arg;
628 rc = assuan_transact (ctrl->scd_local->ctx, "LEARN --force",
629 NULL, NULL, NULL, NULL,
630 learn_status_cb, &parm);
631 if (rc)
632 return unlock_scd (ctrl, rc);
634 return unlock_scd (ctrl, 0);
639 static gpg_error_t
640 get_serialno_cb (void *opaque, const char *line)
642 char **serialno = opaque;
643 const char *keyword = line;
644 const char *s;
645 int keywordlen, n;
647 for (keywordlen=0; *line && !spacep (line); line++, keywordlen++)
649 while (spacep (line))
650 line++;
652 if (keywordlen == 8 && !memcmp (keyword, "SERIALNO", keywordlen))
654 if (*serialno)
655 return gpg_error (GPG_ERR_CONFLICT); /* Unexpected status line. */
656 for (n=0,s=line; hexdigitp (s); s++, n++)
658 if (!n || (n&1)|| !(spacep (s) || !*s) )
659 return gpg_error (GPG_ERR_ASS_PARAMETER);
660 *serialno = xtrymalloc (n+1);
661 if (!*serialno)
662 return out_of_core ();
663 memcpy (*serialno, line, n);
664 (*serialno)[n] = 0;
667 return 0;
670 /* Return the serial number of the card or an appropriate error. The
671 serial number is returned as a hexstring. */
673 agent_card_serialno (ctrl_t ctrl, char **r_serialno)
675 int rc;
676 char *serialno = NULL;
678 rc = start_scd (ctrl);
679 if (rc)
680 return rc;
682 rc = assuan_transact (ctrl->scd_local->ctx, "SERIALNO",
683 NULL, NULL, NULL, NULL,
684 get_serialno_cb, &serialno);
685 if (rc)
687 xfree (serialno);
688 return unlock_scd (ctrl, rc);
690 *r_serialno = serialno;
691 return unlock_scd (ctrl, 0);
697 static gpg_error_t
698 membuf_data_cb (void *opaque, const void *buffer, size_t length)
700 membuf_t *data = opaque;
702 if (buffer)
703 put_membuf (data, buffer, length);
704 return 0;
707 /* Handle the NEEDPIN inquiry. */
708 static gpg_error_t
709 inq_needpin (void *opaque, const char *line)
711 struct inq_needpin_s *parm = opaque;
712 char *pin;
713 size_t pinlen;
714 int rc;
716 if (!strncmp (line, "NEEDPIN", 7) && (line[7] == ' ' || !line[7]))
718 line += 7;
719 while (*line == ' ')
720 line++;
722 pinlen = 90;
723 pin = gcry_malloc_secure (pinlen);
724 if (!pin)
725 return out_of_core ();
727 rc = parm->getpin_cb (parm->getpin_cb_arg, line, pin, pinlen);
728 if (!rc)
729 rc = assuan_send_data (parm->ctx, pin, pinlen);
730 xfree (pin);
732 else if (!strncmp (line, "POPUPKEYPADPROMPT", 17)
733 && (line[17] == ' ' || !line[17]))
735 line += 17;
736 while (*line == ' ')
737 line++;
739 rc = parm->getpin_cb (parm->getpin_cb_arg, line, NULL, 1);
741 else if (!strncmp (line, "DISMISSKEYPADPROMPT", 19)
742 && (line[19] == ' ' || !line[19]))
744 rc = parm->getpin_cb (parm->getpin_cb_arg, "", NULL, 0);
746 else if (parm->passthru)
748 unsigned char *value;
749 size_t valuelen;
750 int rest;
751 int needrest = !strncmp (line, "KEYDATA", 8);
753 /* Pass the inquiry up to our caller. We limit the maximum
754 amount to an arbitrary value. As we know that the KEYDATA
755 enquiry is pretty sensitive we disable logging then */
756 if ((rest = (needrest
757 && !assuan_get_flag (parm->passthru, ASSUAN_CONFIDENTIAL))))
758 assuan_begin_confidential (parm->passthru);
759 rc = assuan_inquire (parm->passthru, line, &value, &valuelen, 8096);
760 if (rest)
761 assuan_end_confidential (parm->passthru);
762 if (!rc)
764 if ((rest = (needrest
765 && !assuan_get_flag (parm->ctx, ASSUAN_CONFIDENTIAL))))
766 assuan_begin_confidential (parm->ctx);
767 rc = assuan_send_data (parm->ctx, value, valuelen);
768 if (rest)
769 assuan_end_confidential (parm->ctx);
770 xfree (value);
772 else
773 log_error ("error forwarding inquiry `%s': %s\n",
774 line, gpg_strerror (rc));
776 else
778 log_error ("unsupported inquiry `%s'\n", line);
779 rc = gpg_error (GPG_ERR_ASS_UNKNOWN_INQUIRE);
782 return rc;
787 /* Create a signature using the current card */
789 agent_card_pksign (ctrl_t ctrl,
790 const char *keyid,
791 int (*getpin_cb)(void *, const char *, char*, size_t),
792 void *getpin_cb_arg,
793 const unsigned char *indata, size_t indatalen,
794 unsigned char **r_buf, size_t *r_buflen)
796 int rc, i;
797 char *p, line[ASSUAN_LINELENGTH];
798 membuf_t data;
799 struct inq_needpin_s inqparm;
800 size_t len;
801 unsigned char *sigbuf;
802 size_t sigbuflen;
804 *r_buf = NULL;
805 rc = start_scd (ctrl);
806 if (rc)
807 return rc;
809 if (indatalen*2 + 50 > DIM(line))
810 return unlock_scd (ctrl, gpg_error (GPG_ERR_GENERAL));
812 sprintf (line, "SETDATA ");
813 p = line + strlen (line);
814 for (i=0; i < indatalen ; i++, p += 2 )
815 sprintf (p, "%02X", indata[i]);
816 rc = assuan_transact (ctrl->scd_local->ctx, line,
817 NULL, NULL, NULL, NULL, NULL, NULL);
818 if (rc)
819 return unlock_scd (ctrl, rc);
821 init_membuf (&data, 1024);
822 inqparm.ctx = ctrl->scd_local->ctx;
823 inqparm.getpin_cb = getpin_cb;
824 inqparm.getpin_cb_arg = getpin_cb_arg;
825 inqparm.passthru = 0;
826 snprintf (line, DIM(line)-1,
827 ctrl->use_auth_call? "PKAUTH %s":"PKSIGN %s", keyid);
828 line[DIM(line)-1] = 0;
829 rc = assuan_transact (ctrl->scd_local->ctx, line,
830 membuf_data_cb, &data,
831 inq_needpin, &inqparm,
832 NULL, NULL);
833 if (rc)
835 xfree (get_membuf (&data, &len));
836 return unlock_scd (ctrl, rc);
838 sigbuf = get_membuf (&data, &sigbuflen);
840 /* Create an S-expression from it which is formatted like this:
841 "(7:sig-val(3:rsa(1:sSIGBUFLEN:SIGBUF)))" */
842 *r_buflen = 21 + 11 + sigbuflen + 4;
843 p = xtrymalloc (*r_buflen);
844 *r_buf = (unsigned char*)p;
845 if (!p)
846 return unlock_scd (ctrl, out_of_core ());
847 p = stpcpy (p, "(7:sig-val(3:rsa(1:s" );
848 sprintf (p, "%u:", (unsigned int)sigbuflen);
849 p += strlen (p);
850 memcpy (p, sigbuf, sigbuflen);
851 p += sigbuflen;
852 strcpy (p, ")))");
853 xfree (sigbuf);
855 assert (gcry_sexp_canon_len (*r_buf, *r_buflen, NULL, NULL));
856 return unlock_scd (ctrl, 0);
859 /* Decipher INDATA using the current card. Note that the returned value is */
861 agent_card_pkdecrypt (ctrl_t ctrl,
862 const char *keyid,
863 int (*getpin_cb)(void *, const char *, char*, size_t),
864 void *getpin_cb_arg,
865 const unsigned char *indata, size_t indatalen,
866 char **r_buf, size_t *r_buflen)
868 int rc, i;
869 char *p, line[ASSUAN_LINELENGTH];
870 membuf_t data;
871 struct inq_needpin_s inqparm;
872 size_t len;
874 *r_buf = NULL;
875 rc = start_scd (ctrl);
876 if (rc)
877 return rc;
879 /* FIXME: use secure memory where appropriate */
880 if (indatalen*2 + 50 > DIM(line))
881 return unlock_scd (ctrl, gpg_error (GPG_ERR_GENERAL));
883 sprintf (line, "SETDATA ");
884 p = line + strlen (line);
885 for (i=0; i < indatalen ; i++, p += 2 )
886 sprintf (p, "%02X", indata[i]);
887 rc = assuan_transact (ctrl->scd_local->ctx, line,
888 NULL, NULL, NULL, NULL, NULL, NULL);
889 if (rc)
890 return unlock_scd (ctrl, rc);
892 init_membuf (&data, 1024);
893 inqparm.ctx = ctrl->scd_local->ctx;
894 inqparm.getpin_cb = getpin_cb;
895 inqparm.getpin_cb_arg = getpin_cb_arg;
896 inqparm.passthru = 0;
897 snprintf (line, DIM(line)-1, "PKDECRYPT %s", keyid);
898 line[DIM(line)-1] = 0;
899 rc = assuan_transact (ctrl->scd_local->ctx, line,
900 membuf_data_cb, &data,
901 inq_needpin, &inqparm,
902 NULL, NULL);
903 if (rc)
905 xfree (get_membuf (&data, &len));
906 return unlock_scd (ctrl, rc);
908 *r_buf = get_membuf (&data, r_buflen);
909 if (!*r_buf)
910 return unlock_scd (ctrl, gpg_error (GPG_ERR_ENOMEM));
912 return unlock_scd (ctrl, 0);
917 /* Read a certificate with ID into R_BUF and R_BUFLEN. */
919 agent_card_readcert (ctrl_t ctrl,
920 const char *id, char **r_buf, size_t *r_buflen)
922 int rc;
923 char line[ASSUAN_LINELENGTH];
924 membuf_t data;
925 size_t len;
927 *r_buf = NULL;
928 rc = start_scd (ctrl);
929 if (rc)
930 return rc;
932 init_membuf (&data, 1024);
933 snprintf (line, DIM(line)-1, "READCERT %s", id);
934 line[DIM(line)-1] = 0;
935 rc = assuan_transact (ctrl->scd_local->ctx, line,
936 membuf_data_cb, &data,
937 NULL, NULL,
938 NULL, NULL);
939 if (rc)
941 xfree (get_membuf (&data, &len));
942 return unlock_scd (ctrl, rc);
944 *r_buf = get_membuf (&data, r_buflen);
945 if (!*r_buf)
946 return unlock_scd (ctrl, gpg_error (GPG_ERR_ENOMEM));
948 return unlock_scd (ctrl, 0);
953 /* Read a key with ID and return it in an allocate buffer pointed to
954 by r_BUF as a valid S-expression. */
956 agent_card_readkey (ctrl_t ctrl, const char *id, unsigned char **r_buf)
958 int rc;
959 char line[ASSUAN_LINELENGTH];
960 membuf_t data;
961 size_t len, buflen;
963 *r_buf = NULL;
964 rc = start_scd (ctrl);
965 if (rc)
966 return rc;
968 init_membuf (&data, 1024);
969 snprintf (line, DIM(line)-1, "READKEY %s", id);
970 line[DIM(line)-1] = 0;
971 rc = assuan_transact (ctrl->scd_local->ctx, line,
972 membuf_data_cb, &data,
973 NULL, NULL,
974 NULL, NULL);
975 if (rc)
977 xfree (get_membuf (&data, &len));
978 return unlock_scd (ctrl, rc);
980 *r_buf = get_membuf (&data, &buflen);
981 if (!*r_buf)
982 return unlock_scd (ctrl, gpg_error (GPG_ERR_ENOMEM));
984 if (!gcry_sexp_canon_len (*r_buf, buflen, NULL, NULL))
986 xfree (*r_buf); *r_buf = NULL;
987 return unlock_scd (ctrl, gpg_error (GPG_ERR_INV_VALUE));
990 return unlock_scd (ctrl, 0);
995 /* Type used with the card_getattr_cb. */
996 struct card_getattr_parm_s {
997 const char *keyword; /* Keyword to look for. */
998 size_t keywordlen; /* strlen of KEYWORD. */
999 char *data; /* Malloced and unescaped data. */
1000 int error; /* ERRNO value or 0 on success. */
1003 /* Callback function for agent_card_getattr. */
1004 static gpg_error_t
1005 card_getattr_cb (void *opaque, const char *line)
1007 struct card_getattr_parm_s *parm = opaque;
1008 const char *keyword = line;
1009 int keywordlen;
1011 if (parm->data)
1012 return 0; /* We want only the first occurrence. */
1014 for (keywordlen=0; *line && !spacep (line); line++, keywordlen++)
1016 while (spacep (line))
1017 line++;
1019 if (keywordlen == parm->keywordlen
1020 && !memcmp (keyword, parm->keyword, keywordlen))
1022 parm->data = percent_plus_unescape ((const unsigned char*)line, 0xff);
1023 if (!parm->data)
1024 parm->error = errno;
1027 return 0;
1031 /* Call the agent to retrieve a single line data object. On success
1032 the object is malloced and stored at RESULT; it is guaranteed that
1033 NULL is never stored in this case. On error an error code is
1034 returned and NULL stored at RESULT. */
1035 gpg_error_t
1036 agent_card_getattr (ctrl_t ctrl, const char *name, char **result)
1038 int err;
1039 struct card_getattr_parm_s parm;
1040 char line[ASSUAN_LINELENGTH];
1042 *result = NULL;
1044 if (!*name)
1045 return gpg_error (GPG_ERR_INV_VALUE);
1047 memset (&parm, 0, sizeof parm);
1048 parm.keyword = name;
1049 parm.keywordlen = strlen (name);
1051 /* We assume that NAME does not need escaping. */
1052 if (8 + strlen (name) > DIM(line)-1)
1053 return gpg_error (GPG_ERR_TOO_LARGE);
1054 stpcpy (stpcpy (line, "GETATTR "), name);
1056 err = start_scd (ctrl);
1057 if (err)
1058 return err;
1060 err = assuan_transact (ctrl->scd_local->ctx, line,
1061 NULL, NULL, NULL, NULL,
1062 card_getattr_cb, &parm);
1063 if (!err && parm.error)
1064 err = gpg_error_from_errno (parm.error);
1066 if (!err && !parm.data)
1067 err = gpg_error (GPG_ERR_NO_DATA);
1069 if (!err)
1070 *result = parm.data;
1071 else
1072 xfree (parm.data);
1074 return unlock_scd (ctrl, err);
1080 static gpg_error_t
1081 pass_status_thru (void *opaque, const char *line)
1083 assuan_context_t ctx = opaque;
1084 char keyword[200];
1085 int i;
1087 for (i=0; *line && !spacep (line) && i < DIM(keyword)-1; line++, i++)
1088 keyword[i] = *line;
1089 keyword[i] = 0;
1090 /* truncate any remaining keyword stuff. */
1091 for (; *line && !spacep (line); line++)
1093 while (spacep (line))
1094 line++;
1096 assuan_write_status (ctx, keyword, line);
1097 return 0;
1100 static gpg_error_t
1101 pass_data_thru (void *opaque, const void *buffer, size_t length)
1103 assuan_context_t ctx = opaque;
1105 assuan_send_data (ctx, buffer, length);
1106 return 0;
1110 /* Send the line CMDLINE with command for the SCDdaemon to it and send
1111 all status messages back. This command is used as a general quoting
1112 mechanism to pass everything verbatim to SCDAEMON. The PIN
1113 inquiry is handled inside gpg-agent. */
1115 agent_card_scd (ctrl_t ctrl, const char *cmdline,
1116 int (*getpin_cb)(void *, const char *, char*, size_t),
1117 void *getpin_cb_arg, void *assuan_context)
1119 int rc;
1120 struct inq_needpin_s inqparm;
1122 rc = start_scd (ctrl);
1123 if (rc)
1124 return rc;
1126 inqparm.ctx = ctrl->scd_local->ctx;
1127 inqparm.getpin_cb = getpin_cb;
1128 inqparm.getpin_cb_arg = getpin_cb_arg;
1129 inqparm.passthru = assuan_context;
1130 rc = assuan_transact (ctrl->scd_local->ctx, cmdline,
1131 pass_data_thru, assuan_context,
1132 inq_needpin, &inqparm,
1133 pass_status_thru, assuan_context);
1134 if (rc)
1136 return unlock_scd (ctrl, rc);
1139 return unlock_scd (ctrl, 0);