2007-07-05 Marcus Brinkmann <marcus@g10code.de>
[gnupg.git] / g10 / call-agent.c
blob55e7f24ffc9fae6ae4d7f6b395240068087951e7
1 /* call-agent.c - divert operations to the agent
2 * Copyright (C) 2001, 2002, 2003, 2006 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 <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <errno.h>
25 #include <unistd.h>
26 #include <time.h>
27 #include <assert.h>
28 #ifdef HAVE_LOCALE_H
29 #include <locale.h>
30 #endif
31 #include <assuan.h>
33 #include "gpg.h"
34 #include "util.h"
35 #include "membuf.h"
36 #include "options.h"
37 #include "i18n.h"
38 #include "asshelp.h"
39 #include "call-agent.h"
41 #ifndef DBG_ASSUAN
42 # define DBG_ASSUAN 1
43 #endif
45 static assuan_context_t agent_ctx = NULL;
47 struct cipher_parm_s
49 assuan_context_t ctx;
50 const char *ciphertext;
51 size_t ciphertextlen;
54 struct writekey_parm_s
56 assuan_context_t ctx;
57 const unsigned char *keydata;
58 size_t keydatalen;
61 struct genkey_parm_s
63 assuan_context_t ctx;
64 const char *sexp;
65 size_t sexplen;
70 /* Try to connect to the agent via socket or fork it off and work by
71 pipes. Handle the server's initial greeting */
72 static int
73 start_agent (void)
75 if (agent_ctx)
76 return 0; /* Fixme: We need a context for each thread or serialize
77 the access to the agent. */
79 return start_new_gpg_agent (&agent_ctx,
80 GPG_ERR_SOURCE_DEFAULT,
81 opt.homedir,
82 opt.agent_program,
83 opt.display, opt.ttyname, opt.ttytype,
84 opt.lc_ctype, opt.lc_messages,
85 opt.verbose, DBG_ASSUAN,
86 NULL, NULL);
90 /* Return a new malloced string by unescaping the string S. Escaping
91 is percent escaping and '+'/space mapping. A binary nul will
92 silently be replaced by a 0xFF. Function returns NULL to indicate
93 an out of memory status. */
94 static char *
95 unescape_status_string (const unsigned char *s)
97 char *buffer, *d;
99 buffer = d = xtrymalloc (strlen (s)+1);
100 if (!buffer)
101 return NULL;
102 while (*s)
104 if (*s == '%' && s[1] && s[2])
106 s++;
107 *d = xtoi_2 (s);
108 if (!*d)
109 *d = '\xff';
110 d++;
111 s += 2;
113 else if (*s == '+')
115 *d++ = ' ';
116 s++;
118 else
119 *d++ = *s++;
121 *d = 0;
122 return buffer;
125 /* Copy the text ATEXT into the buffer P and do plus '+' and percent
126 escaping. Note that the provided buffer needs to be 3 times the
127 size of ATEXT plus 1. Returns a pointer to the leading Nul in P. */
128 static char *
129 percent_plus_escape (char *p, const char *atext)
131 const unsigned char *s;
133 for (s=atext; *s; s++)
135 if (*s < ' ' || *s == '+')
137 sprintf (p, "%%%02X", *s);
138 p += 3;
140 else if (*s == ' ')
141 *p++ = '+';
142 else
143 *p++ = *s;
145 *p = 0;
146 return p;
149 /* Take a 20 byte hexencoded string and put it into the the provided
150 20 byte buffer FPR in binary format. */
151 static int
152 unhexify_fpr (const char *hexstr, unsigned char *fpr)
154 const char *s;
155 int n;
157 for (s=hexstr, n=0; hexdigitp (s); s++, n++)
159 if (*s || (n != 40))
160 return 0; /* no fingerprint (invalid or wrong length). */
161 n /= 2;
162 for (s=hexstr, n=0; *s; s += 2, n++)
163 fpr[n] = xtoi_2 (s);
164 return 1; /* okay */
167 /* Take the serial number from LINE and return it verbatim in a newly
168 allocated string. We make sure that only hex characters are
169 returned. */
170 static char *
171 store_serialno (const char *line)
173 const char *s;
174 char *p;
176 for (s=line; hexdigitp (s); s++)
178 p = xtrymalloc (s + 1 - line);
179 if (p)
181 memcpy (p, line, s-line);
182 p[s-line] = 0;
184 return p;
189 /* Release the card info structure INFO. */
190 void
191 agent_release_card_info (struct agent_card_info_s *info)
193 if (!info)
194 return;
196 xfree (info->serialno); info->serialno = NULL;
197 xfree (info->disp_name); info->disp_name = NULL;
198 xfree (info->disp_lang); info->disp_lang = NULL;
199 xfree (info->pubkey_url); info->pubkey_url = NULL;
200 xfree (info->login_data); info->login_data = NULL;
201 info->cafpr1valid = info->cafpr2valid = info->cafpr3valid = 0;
202 info->fpr1valid = info->fpr2valid = info->fpr3valid = 0;
205 static int
206 learn_status_cb (void *opaque, const char *line)
208 struct agent_card_info_s *parm = opaque;
209 const char *keyword = line;
210 int keywordlen;
211 int i;
213 for (keywordlen=0; *line && !spacep (line); line++, keywordlen++)
215 while (spacep (line))
216 line++;
218 if (keywordlen == 8 && !memcmp (keyword, "SERIALNO", keywordlen))
220 xfree (parm->serialno);
221 parm->serialno = store_serialno (line);
223 else if (keywordlen == 9 && !memcmp (keyword, "DISP-NAME", keywordlen))
225 xfree (parm->disp_name);
226 parm->disp_name = unescape_status_string (line);
228 else if (keywordlen == 9 && !memcmp (keyword, "DISP-LANG", keywordlen))
230 xfree (parm->disp_lang);
231 parm->disp_lang = unescape_status_string (line);
233 else if (keywordlen == 8 && !memcmp (keyword, "DISP-SEX", keywordlen))
235 parm->disp_sex = *line == '1'? 1 : *line == '2' ? 2: 0;
237 else if (keywordlen == 10 && !memcmp (keyword, "PUBKEY-URL", keywordlen))
239 xfree (parm->pubkey_url);
240 parm->pubkey_url = unescape_status_string (line);
242 else if (keywordlen == 10 && !memcmp (keyword, "LOGIN-DATA", keywordlen))
244 xfree (parm->login_data);
245 parm->login_data = unescape_status_string (line);
247 else if (keywordlen == 11 && !memcmp (keyword, "SIG-COUNTER", keywordlen))
249 parm->sig_counter = strtoul (line, NULL, 0);
251 else if (keywordlen == 10 && !memcmp (keyword, "CHV-STATUS", keywordlen))
253 char *p, *buf;
255 buf = p = unescape_status_string (line);
256 if (buf)
258 while (spacep (p))
259 p++;
260 parm->chv1_cached = atoi (p);
261 while (*p && !spacep (p))
262 p++;
263 while (spacep (p))
264 p++;
265 for (i=0; *p && i < 3; i++)
267 parm->chvmaxlen[i] = atoi (p);
268 while (*p && !spacep (p))
269 p++;
270 while (spacep (p))
271 p++;
273 for (i=0; *p && i < 3; i++)
275 parm->chvretry[i] = atoi (p);
276 while (*p && !spacep (p))
277 p++;
278 while (spacep (p))
279 p++;
281 xfree (buf);
284 else if (keywordlen == 7 && !memcmp (keyword, "KEY-FPR", keywordlen))
286 int no = atoi (line);
287 while (*line && !spacep (line))
288 line++;
289 while (spacep (line))
290 line++;
291 if (no == 1)
292 parm->fpr1valid = unhexify_fpr (line, parm->fpr1);
293 else if (no == 2)
294 parm->fpr2valid = unhexify_fpr (line, parm->fpr2);
295 else if (no == 3)
296 parm->fpr3valid = unhexify_fpr (line, parm->fpr3);
298 else if (keywordlen == 6 && !memcmp (keyword, "CA-FPR", keywordlen))
300 int no = atoi (line);
301 while (*line && !spacep (line))
302 line++;
303 while (spacep (line))
304 line++;
305 if (no == 1)
306 parm->cafpr1valid = unhexify_fpr (line, parm->cafpr1);
307 else if (no == 2)
308 parm->cafpr2valid = unhexify_fpr (line, parm->cafpr2);
309 else if (no == 3)
310 parm->cafpr3valid = unhexify_fpr (line, parm->cafpr3);
313 return 0;
316 /* Call the agent to learn about a smartcard */
318 agent_learn (struct agent_card_info_s *info)
320 int rc;
322 rc = start_agent ();
323 if (rc)
324 return rc;
326 memset (info, 0, sizeof *info);
327 rc = assuan_transact (agent_ctx, "LEARN --send",
328 NULL, NULL, NULL, NULL,
329 learn_status_cb, info);
331 return rc;
334 /* Call the agent to retrieve a data object. This function returns
335 the data in the same structure as used by the learn command. It is
336 allowed to update such a structure using this commmand. */
338 agent_scd_getattr (const char *name, struct agent_card_info_s *info)
340 int rc;
341 char line[ASSUAN_LINELENGTH];
343 if (!*name)
344 return gpg_error (GPG_ERR_INV_VALUE);
346 /* We assume that NAME does not need escaping. */
347 if (12 + strlen (name) > DIM(line)-1)
348 return gpg_error (GPG_ERR_TOO_LARGE);
349 stpcpy (stpcpy (line, "SCD GETATTR "), name);
351 rc = start_agent ();
352 if (rc)
353 return rc;
355 rc = assuan_transact (agent_ctx, line, NULL, NULL, NULL, NULL,
356 learn_status_cb, info);
358 return rc;
362 /* Send an setattr command to the SCdaemon. SERIALNO is not actually
363 used here but required by gpg 1.4's implementation of this code in
364 cardglue.c. */
366 agent_scd_setattr (const char *name,
367 const unsigned char *value, size_t valuelen,
368 const char *serialno)
370 int rc;
371 char line[ASSUAN_LINELENGTH];
372 char *p;
374 if (!*name || !valuelen)
375 return gpg_error (GPG_ERR_INV_VALUE);
377 /* We assume that NAME does not need escaping. */
378 if (12 + strlen (name) > DIM(line)-1)
379 return gpg_error (GPG_ERR_TOO_LARGE);
381 p = stpcpy (stpcpy (line, "SCD SETATTR "), name);
382 *p++ = ' ';
383 for (; valuelen; value++, valuelen--)
385 if (p >= line + DIM(line)-5 )
386 return gpg_error (GPG_ERR_TOO_LARGE);
387 if (*value < ' ' || *value == '+' || *value == '%')
389 sprintf (p, "%%%02X", *value);
390 p += 3;
392 else if (*value == ' ')
393 *p++ = '+';
394 else
395 *p++ = *value;
397 *p = 0;
399 rc = start_agent ();
400 if (rc)
401 return rc;
403 rc = assuan_transact (agent_ctx, line, NULL, NULL, NULL, NULL, NULL, NULL);
404 return rc;
409 /* Handle a KEYDATA inquiry. Note, we only send the data,
410 assuan_transact takes care of flushing and writing the end */
411 static assuan_error_t
412 inq_writekey_parms (void *opaque, const char *keyword)
414 struct writekey_parm_s *parm = opaque;
416 return assuan_send_data (parm->ctx, parm->keydata, parm->keydatalen);
420 /* Send a WRITEKEY command to the SCdaemon. */
421 int
422 agent_scd_writekey (int keyno, const char *serialno,
423 const unsigned char *keydata, size_t keydatalen)
425 int rc;
426 char line[ASSUAN_LINELENGTH];
427 struct writekey_parm_s parms;
429 rc = start_agent ();
430 if (rc)
431 return rc;
433 memset (&parms, 0, sizeof parms);
435 snprintf (line, DIM(line)-1, "SCD WRITEKEY --force OPENPGP.%d", keyno);
436 line[DIM(line)-1] = 0;
437 parms.ctx = agent_ctx;
438 parms.keydata = keydata;
439 parms.keydatalen = keydatalen;
441 rc = assuan_transact (agent_ctx, line, NULL, NULL,
442 inq_writekey_parms, &parms, NULL, NULL);
444 return rc;
450 /* Status callback for the SCD GENKEY command. */
451 static int
452 scd_genkey_cb (void *opaque, const char *line)
454 struct agent_card_genkey_s *parm = opaque;
455 const char *keyword = line;
456 int keywordlen;
457 gpg_error_t rc;
459 log_debug ("got status line `%s'\n", line);
460 for (keywordlen=0; *line && !spacep (line); line++, keywordlen++)
462 while (spacep (line))
463 line++;
465 if (keywordlen == 7 && !memcmp (keyword, "KEY-FPR", keywordlen))
467 parm->fprvalid = unhexify_fpr (line, parm->fpr);
469 if (keywordlen == 8 && !memcmp (keyword, "KEY-DATA", keywordlen))
471 gcry_mpi_t a;
472 const char *name = line;
474 while (*line && !spacep (line))
475 line++;
476 while (spacep (line))
477 line++;
479 rc = gcry_mpi_scan (&a, GCRYMPI_FMT_HEX, line, 0, NULL);
480 if (rc)
481 log_error ("error parsing received key data: %s\n", gpg_strerror (rc));
482 else if (*name == 'n' && spacep (name+1))
483 parm->n = a;
484 else if (*name == 'e' && spacep (name+1))
485 parm->e = a;
486 else
488 log_info ("unknown parameter name in received key data\n");
489 gcry_mpi_release (a);
492 else if (keywordlen == 14 && !memcmp (keyword,"KEY-CREATED-AT", keywordlen))
494 parm->created_at = (u32)strtoul (line, NULL, 10);
497 return 0;
500 /* Send a GENKEY command to the SCdaemon. SERIALNO is not used in
501 this implementation. */
503 agent_scd_genkey (struct agent_card_genkey_s *info, int keyno, int force,
504 const char *serialno)
506 int rc;
507 char line[ASSUAN_LINELENGTH];
509 rc = start_agent ();
510 if (rc)
511 return rc;
513 memset (info, 0, sizeof *info);
514 snprintf (line, DIM(line)-1, "SCD GENKEY %s%d",
515 force? "--force ":"", keyno);
516 line[DIM(line)-1] = 0;
518 memset (info, 0, sizeof *info);
519 rc = assuan_transact (agent_ctx, line,
520 NULL, NULL, NULL, NULL,
521 scd_genkey_cb, info);
523 return rc;
527 static int
528 membuf_data_cb (void *opaque, const void *buffer, size_t length)
530 membuf_t *data = opaque;
532 if (buffer)
533 put_membuf (data, buffer, length);
534 return 0;
537 /* Send a sign command to the scdaemon via gpg-agent's pass thru
538 mechanism. */
540 agent_scd_pksign (const char *serialno, int hashalgo,
541 const unsigned char *indata, size_t indatalen,
542 unsigned char **r_buf, size_t *r_buflen)
544 int rc, i;
545 char *p, line[ASSUAN_LINELENGTH];
546 membuf_t data;
547 size_t len;
549 /* Note, hashalgo is not yet used but hardwired to SHA1 in SCdaemon. */
551 *r_buf = NULL;
552 *r_buflen = 0;
554 rc = start_agent ();
555 if (rc)
556 return rc;
558 if (indatalen*2 + 50 > DIM(line))
559 return gpg_error (GPG_ERR_GENERAL);
561 sprintf (line, "SCD SETDATA ");
562 p = line + strlen (line);
563 for (i=0; i < indatalen ; i++, p += 2 )
564 sprintf (p, "%02X", indata[i]);
565 rc = assuan_transact (agent_ctx, line, NULL, NULL, NULL, NULL, NULL, NULL);
566 if (rc)
567 return rc;
569 init_membuf (&data, 1024);
570 #if 0
571 if (!hashalgo) /* Temporary test hack. */
572 snprintf (line, DIM(line)-1, "SCD PKAUTH %s", serialno);
573 else
574 #endif
575 snprintf (line, DIM(line)-1, "SCD PKSIGN %s%s",
576 hashalgo == GCRY_MD_RMD160? "--hash=rmd160 " : "",
577 serialno);
578 line[DIM(line)-1] = 0;
579 rc = assuan_transact (agent_ctx, line, membuf_data_cb, &data,
580 NULL, NULL, NULL, NULL);
581 if (rc)
583 xfree (get_membuf (&data, &len));
584 return rc;
586 *r_buf = get_membuf (&data, r_buflen);
588 return 0;
592 /* Decrypt INDATA of length INDATALEN using the card identified by
593 SERIALNO. Return the plaintext in a nwly allocated buffer stored
594 at the address of R_BUF.
596 Note, we currently support only RSA or more exactly algorithms
597 taking one input data element. */
599 agent_scd_pkdecrypt (const char *serialno,
600 const unsigned char *indata, size_t indatalen,
601 unsigned char **r_buf, size_t *r_buflen)
603 int rc, i;
604 char *p, line[ASSUAN_LINELENGTH];
605 membuf_t data;
606 size_t len;
608 *r_buf = NULL;
609 rc = start_agent ();
610 if (rc)
611 return rc;
613 /* FIXME: use secure memory where appropriate */
614 if (indatalen*2 + 50 > DIM(line))
615 return gpg_error (GPG_ERR_GENERAL);
617 sprintf (line, "SCD SETDATA ");
618 p = line + strlen (line);
619 for (i=0; i < indatalen ; i++, p += 2 )
620 sprintf (p, "%02X", indata[i]);
621 rc = assuan_transact (agent_ctx, line, NULL, NULL, NULL, NULL, NULL, NULL);
622 if (rc)
623 return rc;
625 init_membuf (&data, 1024);
626 snprintf (line, DIM(line)-1, "SCD PKDECRYPT %s", serialno);
627 line[DIM(line)-1] = 0;
628 rc = assuan_transact (agent_ctx, line,
629 membuf_data_cb, &data,
630 NULL, NULL, NULL, NULL);
631 if (rc)
633 xfree (get_membuf (&data, &len));
634 return rc;
636 *r_buf = get_membuf (&data, r_buflen);
637 if (!*r_buf)
638 return gpg_error (GPG_ERR_ENOMEM);
640 return 0;
644 /* Change the PIN of an OpenPGP card or reset the retry counter.
645 CHVNO 1: Change the PIN
646 2: Same as 1
647 3: Change the admin PIN
648 101: Set a new PIN and reset the retry counter
649 102: Same as 101
650 SERIALNO is not used.
653 agent_scd_change_pin (int chvno, const char *serialno)
655 int rc;
656 char line[ASSUAN_LINELENGTH];
657 const char *reset = "";
659 if (chvno >= 100)
660 reset = "--reset";
661 chvno %= 100;
663 rc = start_agent ();
664 if (rc)
665 return rc;
667 snprintf (line, DIM(line)-1, "SCD PASSWD %s %d", reset, chvno);
668 line[DIM(line)-1] = 0;
669 rc = assuan_transact (agent_ctx, line, NULL, NULL,
670 NULL, NULL, NULL, NULL);
671 return rc;
675 /* Perform a CHECKPIN operation. SERIALNO should be the serial
676 number of the card - optionally followed by the fingerprint;
677 however the fingerprint is ignored here. */
679 agent_scd_checkpin (const char *serialno)
681 int rc;
682 char line[ASSUAN_LINELENGTH];
684 rc = start_agent ();
685 if (rc)
686 return rc;
688 snprintf (line, DIM(line)-1, "SCD CHECKPIN %s", serialno);
689 line[DIM(line)-1] = 0;
690 return assuan_transact (agent_ctx, line,
691 NULL, NULL,
692 NULL, NULL, NULL, NULL);
696 /* Dummy function, only used by the gpg 1.4 implementation. */
697 void
698 agent_clear_pin_cache (const char *sn)
706 /* Note: All strings shall be UTF-8. On success the caler needs to
707 free the string stored at R_PASSPHRASE. On error NULL will be
708 stored at R_PASSPHRASE and an appropriate fpf error code
709 returned. */
710 gpg_error_t
711 agent_get_passphrase (const char *cache_id,
712 const char *err_msg,
713 const char *prompt,
714 const char *desc_msg,
715 char **r_passphrase)
717 int rc;
718 char *line, *p;
719 char cmd[] = "GET_PASSPHRASE --data -- ";
720 membuf_t data;
722 *r_passphrase = NULL;
724 rc = start_agent ();
725 if (rc)
726 return rc;
728 /* We allocate 3 times the needed space for the texts so that
729 there is enough space for escaping. */
730 line = xtrymalloc ( strlen (cmd) + 1
731 + (cache_id? 3*strlen (cache_id): 1) + 1
732 + (err_msg? 3*strlen (err_msg): 1) + 1
733 + (prompt? 3*strlen (prompt): 1) + 1
734 + (desc_msg? 3*strlen (desc_msg): 1) + 1
735 + 1);
736 if (!line)
737 return gpg_error_from_syserror ();
739 p = stpcpy (line, cmd);
740 if (cache_id && *cache_id)
741 p = percent_plus_escape (p, cache_id);
742 else
743 *p++ = 'X';
744 *p++ = ' ';
746 if (err_msg && *err_msg)
747 p = percent_plus_escape (p, err_msg);
748 else
749 *p++ = 'X';
750 *p++ = ' ';
752 if (prompt && *prompt)
753 p = percent_plus_escape (p, prompt);
754 else
755 *p++ = 'X';
756 *p++ = ' ';
758 if (desc_msg && *desc_msg)
759 p = percent_plus_escape (p, desc_msg);
760 else
761 *p++ = 'X';
762 *p = 0;
764 init_membuf_secure (&data, 64);
765 rc = assuan_transact (agent_ctx, line,
766 membuf_data_cb, &data, NULL, NULL, NULL, NULL);
768 if (rc)
769 xfree (get_membuf (&data, NULL));
770 else
772 put_membuf (&data, "", 1);
773 *r_passphrase = get_membuf (&data, NULL);
774 if (!*r_passphrase)
775 rc = gpg_error_from_syserror ();
777 xfree (line);
778 return rc;
782 gpg_error_t
783 agent_clear_passphrase (const char *cache_id)
785 int rc;
786 char line[ASSUAN_LINELENGTH];
788 if (!cache_id || !*cache_id)
789 return 0;
791 rc = start_agent ();
792 if (rc)
793 return rc;
795 snprintf (line, DIM(line)-1, "CLEAR_PASSPHRASE %s", cache_id);
796 line[DIM(line)-1] = 0;
797 return assuan_transact (agent_ctx, line, NULL, NULL, NULL, NULL, NULL, NULL);