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 2 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, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
22 #if 0 /* let Emacs display a red warning */
23 #error fixme: this shares a lot of code with the file in ../sm
44 #include "call-agent.h"
50 static assuan_context_t agent_ctx
= NULL
;
51 static int force_pipe_server
;
56 const char *ciphertext
;
60 struct writekey_parm_s
63 const unsigned char *keydata
;
76 /* Try to connect to the agent via socket or fork it off and work by
77 pipes. Handle the server's initial greeting */
84 char *dft_display
= NULL
;
85 char *dft_ttyname
= NULL
;
86 char *dft_ttytype
= NULL
;
91 return 0; /* fixme: We need a context for each thread or serialize
92 the access to the agent. */
94 infostr
= force_pipe_server
? NULL
: getenv ("GPG_AGENT_INFO");
95 if (!infostr
|| !*infostr
)
103 log_info (_("no running gpg-agent - starting one\n"));
107 gpg_error_t tmperr
= gpg_error_from_errno (errno
);
108 log_error ("error flushing pending output: %s\n", strerror (errno
));
112 if (!opt
.agent_program
|| !*opt
.agent_program
)
113 opt
.agent_program
= GNUPG_DEFAULT_AGENT
;
114 if ( !(pgmname
= strrchr (opt
.agent_program
, '/')))
115 pgmname
= opt
.agent_program
;
120 argv
[1] = "--server";
124 if (log_get_fd () != -1)
125 no_close_list
[i
++] = log_get_fd ();
126 no_close_list
[i
++] = fileno (stderr
);
127 no_close_list
[i
] = -1;
129 /* connect to the agent and perform initial handshaking */
130 rc
= assuan_pipe_connect (&ctx
, opt
.agent_program
, argv
,
138 infostr
= xstrdup (infostr
);
139 if ( !(p
= strchr (infostr
, ':')) || p
== infostr
)
141 log_error (_("malformed GPG_AGENT_INFO environment variable\n"));
143 force_pipe_server
= 1;
144 return start_agent ();
148 while (*p
&& *p
!= ':')
150 prot
= *p
? atoi (p
+1) : 0;
153 log_error (_("gpg-agent protocol version %d is not supported\n"),
156 force_pipe_server
= 1;
157 return start_agent ();
160 rc
= assuan_socket_connect (&ctx
, infostr
, pid
);
162 if (gpg_err_code (rc
) == GPG_ERR_ASS_CONNECT_FAILED
)
164 log_error (_("can't connect to the agent - trying fall back\n"));
165 force_pipe_server
= 1;
166 return start_agent ();
172 log_error ("can't connect to the agent: %s\n", gpg_strerror (rc
));
173 return gpg_error (GPG_ERR_NO_AGENT
);
178 log_debug ("connection to agent established\n");
180 rc
= assuan_transact (agent_ctx
, "RESET", NULL
, NULL
, NULL
, NULL
, NULL
, NULL
);
185 #warning put this code into common/asshelp.c
188 dft_display
= getenv ("DISPLAY");
189 if (opt
.display
|| dft_display
)
192 if (asprintf (&optstr
, "OPTION display=%s",
193 opt
.display
? opt
.display
: dft_display
) < 0)
194 return gpg_error_from_errno (errno
);
195 rc
= assuan_transact (agent_ctx
, optstr
, NULL
, NULL
, NULL
, NULL
, NULL
,
203 dft_ttyname
= getenv ("GPG_TTY");
204 if ((!dft_ttyname
|| !*dft_ttyname
) && ttyname (0))
205 dft_ttyname
= ttyname (0);
207 if (opt
.ttyname
|| dft_ttyname
)
210 if (asprintf (&optstr
, "OPTION ttyname=%s",
211 opt
.ttyname
? opt
.ttyname
: dft_ttyname
) < 0)
212 return gpg_error_from_errno (errno
);
213 rc
= assuan_transact (agent_ctx
, optstr
, NULL
, NULL
, NULL
, NULL
, NULL
,
219 dft_ttytype
= getenv ("TERM");
220 if (opt
.ttytype
|| (dft_ttyname
&& dft_ttytype
))
223 if (asprintf (&optstr
, "OPTION ttytype=%s",
224 opt
.ttyname
? opt
.ttytype
: dft_ttytype
) < 0)
225 return gpg_error_from_errno (errno
);
226 rc
= assuan_transact (agent_ctx
, optstr
, NULL
, NULL
, NULL
, NULL
, NULL
,
232 #if defined(HAVE_SETLOCALE) && defined(LC_CTYPE)
233 old_lc
= setlocale (LC_CTYPE
, NULL
);
236 old_lc
= strdup (old_lc
);
238 return gpg_error_from_errno (errno
);
241 dft_lc
= setlocale (LC_CTYPE
, "");
243 if (opt
.lc_ctype
|| (dft_ttyname
&& dft_lc
))
246 if (asprintf (&optstr
, "OPTION lc-ctype=%s",
247 opt
.lc_ctype
? opt
.lc_ctype
: dft_lc
) < 0)
248 rc
= gpg_error_from_errno (errno
);
251 rc
= assuan_transact (agent_ctx
, optstr
, NULL
, NULL
, NULL
, NULL
, NULL
,
256 #if defined(HAVE_SETLOCALE) && defined(LC_CTYPE)
259 setlocale (LC_CTYPE
, old_lc
);
265 #if defined(HAVE_SETLOCALE) && defined(LC_MESSAGES)
266 old_lc
= setlocale (LC_MESSAGES
, NULL
);
269 old_lc
= strdup (old_lc
);
271 return gpg_error_from_errno (errno
);
273 dft_lc
= setlocale (LC_MESSAGES
, "");
275 if (opt
.lc_messages
|| (dft_ttyname
&& dft_lc
))
278 if (asprintf (&optstr
, "OPTION lc-messages=%s",
279 opt
.lc_messages
? opt
.lc_messages
: dft_lc
) < 0)
280 rc
= gpg_error_from_errno (errno
);
283 rc
= assuan_transact (agent_ctx
, optstr
, NULL
, NULL
, NULL
, NULL
, NULL
,
288 #if defined(HAVE_SETLOCALE) && defined(LC_MESSAGES)
291 setlocale (LC_MESSAGES
, old_lc
);
300 /* Return a new malloced string by unescaping the string S. Escaping
301 is percent escaping and '+'/space mapping. A binary nul will
302 silently be replaced by a 0xFF. Function returns NULL to indicate
303 an out of memory status. */
305 unescape_status_string (const unsigned char *s
)
309 buffer
= d
= xtrymalloc (strlen (s
)+1);
314 if (*s
== '%' && s
[1] && s
[2])
335 /* Take a 20 byte hexencoded string and put it into the the provided
336 20 byte buffer FPR in binary format. */
338 unhexify_fpr (const char *hexstr
, unsigned char *fpr
)
343 for (s
=hexstr
, n
=0; hexdigitp (s
); s
++, n
++)
346 return 0; /* no fingerprint (invalid or wrong length). */
348 for (s
=hexstr
, n
=0; *s
; s
+= 2, n
++)
353 /* Take the serial number from LINE and return it verbatim in a newly
354 allocated string. We make sure that only hex characters are
357 store_serialno (const char *line
)
362 for (s
=line
; hexdigitp (s
); s
++)
364 p
= xtrymalloc (s
+ 1 - line
);
367 memcpy (p
, line
, s
-line
);
376 /* Handle a KEYPARMS inquiry. Note, we only send the data,
377 assuan_transact takes care of flushing and writing the end */
379 inq_genkey_parms (void *opaque
, const char *keyword
)
381 struct genkey_parm_s
*parm
= opaque
;
384 rc
= assuan_send_data (parm
->ctx
, parm
->sexp
, parm
->sexplen
);
390 /* Call the agent to generate a new key */
392 agent_genkey (KsbaConstSexp keyparms
, KsbaSexp
*r_pubkey
)
395 struct genkey_parm_s gk_parm
;
405 rc
= assuan_transact (agent_ctx
, "RESET", NULL
, NULL
,
406 NULL
, NULL
, NULL
, NULL
);
410 init_membuf (&data
, 1024);
411 gk_parm
.ctx
= agent_ctx
;
412 gk_parm
.sexp
= keyparms
;
413 gk_parm
.sexplen
= gcry_sexp_canon_len (keyparms
, 0, NULL
, NULL
);
414 if (!gk_parm
.sexplen
)
415 return gpg_error (GPG_ERR_INV_VALUE
);
416 rc
= assuan_transact (agent_ctx
, "GENKEY",
417 membuf_data_cb
, &data
,
418 inq_genkey_parms
, &gk_parm
, NULL
, NULL
);
421 xfree (get_membuf (&data
, &len
));
424 buf
= get_membuf (&data
, &len
);
426 return gpg_error (GPG_ERR_ENOMEM
);
427 if (!gcry_sexp_canon_len (buf
, len
, NULL
, NULL
))
430 return gpg_error (GPG_ERR_INV_SEXP
);
439 /* Ask the agent whether the corresponding secret key is available for
440 the given keygrip. */
442 agent_havekey (const char *hexkeygrip
)
445 char line
[ASSUAN_LINELENGTH
];
451 if (!hexkeygrip
|| strlen (hexkeygrip
) != 40)
452 return gpg_error (GPG_ERR_INV_VALUE
);
454 snprintf (line
, DIM(line
)-1, "HAVEKEY %s", hexkeygrip
);
455 line
[DIM(line
)-1] = 0;
457 rc
= assuan_transact (agent_ctx
, line
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
);
462 /* Release the card info structure INFO. */
464 agent_release_card_info (struct agent_card_info_s
*info
)
469 xfree (info
->serialno
); info
->serialno
= NULL
;
470 xfree (info
->disp_name
); info
->disp_name
= NULL
;
471 xfree (info
->disp_lang
); info
->disp_lang
= NULL
;
472 xfree (info
->pubkey_url
); info
->pubkey_url
= NULL
;
473 xfree (info
->login_data
); info
->login_data
= NULL
;
474 info
->cafpr1valid
= info
->cafpr2valid
= info
->cafpr3valid
= 0;
475 info
->fpr1valid
= info
->fpr2valid
= info
->fpr3valid
= 0;
479 learn_status_cb (void *opaque
, const char *line
)
481 struct agent_card_info_s
*parm
= opaque
;
482 const char *keyword
= line
;
486 for (keywordlen
=0; *line
&& !spacep (line
); line
++, keywordlen
++)
488 while (spacep (line
))
491 if (keywordlen
== 8 && !memcmp (keyword
, "SERIALNO", keywordlen
))
493 xfree (parm
->serialno
);
494 parm
->serialno
= store_serialno (line
);
496 else if (keywordlen
== 9 && !memcmp (keyword
, "DISP-NAME", keywordlen
))
498 xfree (parm
->disp_name
);
499 parm
->disp_name
= unescape_status_string (line
);
501 else if (keywordlen
== 9 && !memcmp (keyword
, "DISP-LANG", keywordlen
))
503 xfree (parm
->disp_lang
);
504 parm
->disp_lang
= unescape_status_string (line
);
506 else if (keywordlen
== 8 && !memcmp (keyword
, "DISP-SEX", keywordlen
))
508 parm
->disp_sex
= *line
== '1'? 1 : *line
== '2' ? 2: 0;
510 else if (keywordlen
== 10 && !memcmp (keyword
, "PUBKEY-URL", keywordlen
))
512 xfree (parm
->pubkey_url
);
513 parm
->pubkey_url
= unescape_status_string (line
);
515 else if (keywordlen
== 10 && !memcmp (keyword
, "LOGIN-DATA", keywordlen
))
517 xfree (parm
->login_data
);
518 parm
->login_data
= unescape_status_string (line
);
520 else if (keywordlen
== 11 && !memcmp (keyword
, "SIG-COUNTER", keywordlen
))
522 parm
->sig_counter
= strtoul (line
, NULL
, 0);
524 else if (keywordlen
== 10 && !memcmp (keyword
, "CHV-STATUS", keywordlen
))
528 buf
= p
= unescape_status_string (line
);
533 parm
->chv1_cached
= atoi (p
);
534 while (*p
&& !spacep (p
))
538 for (i
=0; *p
&& i
< 3; i
++)
540 parm
->chvmaxlen
[i
] = atoi (p
);
541 while (*p
&& !spacep (p
))
546 for (i
=0; *p
&& i
< 3; i
++)
548 parm
->chvretry
[i
] = atoi (p
);
549 while (*p
&& !spacep (p
))
557 else if (keywordlen
== 7 && !memcmp (keyword
, "KEY-FPR", keywordlen
))
559 int no
= atoi (line
);
560 while (*line
&& !spacep (line
))
562 while (spacep (line
))
565 parm
->fpr1valid
= unhexify_fpr (line
, parm
->fpr1
);
567 parm
->fpr2valid
= unhexify_fpr (line
, parm
->fpr2
);
569 parm
->fpr3valid
= unhexify_fpr (line
, parm
->fpr3
);
571 else if (keywordlen
== 6 && !memcmp (keyword
, "CA-FPR", keywordlen
))
573 int no
= atoi (line
);
574 while (*line
&& !spacep (line
))
576 while (spacep (line
))
579 parm
->cafpr1valid
= unhexify_fpr (line
, parm
->cafpr1
);
581 parm
->cafpr2valid
= unhexify_fpr (line
, parm
->cafpr2
);
583 parm
->cafpr3valid
= unhexify_fpr (line
, parm
->cafpr3
);
589 /* Call the agent to learn about a smartcard */
591 agent_learn (struct agent_card_info_s
*info
)
599 memset (info
, 0, sizeof *info
);
600 rc
= assuan_transact (agent_ctx
, "LEARN --send",
601 NULL
, NULL
, NULL
, NULL
,
602 learn_status_cb
, info
);
607 /* Call the agent to retrieve a data object. This function returns
608 the data in the same structure as used by the learn command. It is
609 allowed to update such a structure using this commmand. */
611 agent_scd_getattr (const char *name
, struct agent_card_info_s
*info
)
614 char line
[ASSUAN_LINELENGTH
];
617 return gpg_error (GPG_ERR_INV_VALUE
);
619 /* We assume that NAME does not need escaping. */
620 if (12 + strlen (name
) > DIM(line
)-1)
621 return gpg_error (GPG_ERR_TOO_LARGE
);
622 stpcpy (stpcpy (line
, "SCD GETATTR "), name
);
628 rc
= assuan_transact (agent_ctx
, line
, NULL
, NULL
, NULL
, NULL
,
629 learn_status_cb
, info
);
635 /* Send an setattr command to the SCdaemon. SERIALNO is not actually
636 used here but required by gpg 1.4's implementation of this code in
639 agent_scd_setattr (const char *name
,
640 const unsigned char *value
, size_t valuelen
,
641 const char *serialno
)
644 char line
[ASSUAN_LINELENGTH
];
647 if (!*name
|| !valuelen
)
648 return gpg_error (GPG_ERR_INV_VALUE
);
650 /* We assume that NAME does not need escaping. */
651 if (12 + strlen (name
) > DIM(line
)-1)
652 return gpg_error (GPG_ERR_TOO_LARGE
);
654 p
= stpcpy (stpcpy (line
, "SCD SETATTR "), name
);
656 for (; valuelen
; value
++, valuelen
--)
658 if (p
>= line
+ DIM(line
)-5 )
659 return gpg_error (GPG_ERR_TOO_LARGE
);
660 if (*value
< ' ' || *value
== '+' || *value
== '%')
662 sprintf (p
, "%%%02X", *value
);
665 else if (*value
== ' ')
676 rc
= assuan_transact (agent_ctx
, line
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
);
682 /* Handle a KEYDATA inquiry. Note, we only send the data,
683 assuan_transact takes care of flushing and writing the end */
684 static assuan_error_t
685 inq_writekey_parms (void *opaque
, const char *keyword
)
687 struct writekey_parm_s
*parm
= opaque
;
689 return assuan_send_data (parm
->ctx
, parm
->keydata
, parm
->keydatalen
);
693 /* Send a WRITEKEY command to the SCdaemon. */
695 agent_scd_writekey (int keyno
, const char *serialno
,
696 const unsigned char *keydata
, size_t keydatalen
)
699 char line
[ASSUAN_LINELENGTH
];
700 struct writekey_parm_s parms
;
706 memset (&parms
, 0, sizeof parms
);
708 snprintf (line
, DIM(line
)-1, "SCD WRITEKEY --force OPENPGP.%d", keyno
);
709 line
[DIM(line
)-1] = 0;
710 parms
.ctx
= agent_ctx
;
711 parms
.keydata
= keydata
;
712 parms
.keydatalen
= keydatalen
;
714 rc
= assuan_transact (agent_ctx
, line
, NULL
, NULL
,
715 inq_writekey_parms
, &parms
, NULL
, NULL
);
723 /* Status callback for the SCD GENKEY command. */
725 scd_genkey_cb (void *opaque
, const char *line
)
727 struct agent_card_genkey_s
*parm
= opaque
;
728 const char *keyword
= line
;
732 log_debug ("got status line `%s'\n", line
);
733 for (keywordlen
=0; *line
&& !spacep (line
); line
++, keywordlen
++)
735 while (spacep (line
))
738 if (keywordlen
== 7 && !memcmp (keyword
, "KEY-FPR", keywordlen
))
740 parm
->fprvalid
= unhexify_fpr (line
, parm
->fpr
);
742 if (keywordlen
== 8 && !memcmp (keyword
, "KEY-DATA", keywordlen
))
745 const char *name
= line
;
747 while (*line
&& !spacep (line
))
749 while (spacep (line
))
752 rc
= gcry_mpi_scan (&a
, GCRYMPI_FMT_HEX
, line
, 0, NULL
);
754 log_error ("error parsing received key data: %s\n", gpg_strerror (rc
));
755 else if (*name
== 'n' && spacep (name
+1))
757 else if (*name
== 'e' && spacep (name
+1))
761 log_info ("unknown parameter name in received key data\n");
762 gcry_mpi_release (a
);
765 else if (keywordlen
== 14 && !memcmp (keyword
,"KEY-CREATED-AT", keywordlen
))
767 parm
->created_at
= (u32
)strtoul (line
, NULL
, 10);
773 /* Send a GENKEY command to the SCdaemon. SERIALNO is not used in
774 this implementation. */
776 agent_scd_genkey (struct agent_card_genkey_s
*info
, int keyno
, int force
,
777 const char *serialno
)
780 char line
[ASSUAN_LINELENGTH
];
786 memset (info
, 0, sizeof *info
);
787 snprintf (line
, DIM(line
)-1, "SCD GENKEY %s%d",
788 force
? "--force ":"", keyno
);
789 line
[DIM(line
)-1] = 0;
791 memset (info
, 0, sizeof *info
);
792 rc
= assuan_transact (agent_ctx
, line
,
793 NULL
, NULL
, NULL
, NULL
,
794 scd_genkey_cb
, info
);
801 membuf_data_cb (void *opaque
, const void *buffer
, size_t length
)
803 membuf_t
*data
= opaque
;
806 put_membuf (data
, buffer
, length
);
810 /* Send a sign command to the scdaemon via gpg-agent's pass thru
813 agent_scd_pksign (const char *serialno
, int hashalgo
,
814 const unsigned char *indata
, size_t indatalen
,
815 unsigned char **r_buf
, size_t *r_buflen
)
818 char *p
, line
[ASSUAN_LINELENGTH
];
822 /* Note, hashalgo is not yet used but hardwired to SHA1 in SCdaemon. */
831 if (indatalen
*2 + 50 > DIM(line
))
832 return gpg_error (GPG_ERR_GENERAL
);
834 sprintf (line
, "SCD SETDATA ");
835 p
= line
+ strlen (line
);
836 for (i
=0; i
< indatalen
; i
++, p
+= 2 )
837 sprintf (p
, "%02X", indata
[i
]);
838 rc
= assuan_transact (agent_ctx
, line
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
);
842 init_membuf (&data
, 1024);
844 if (!hashalgo
) /* Temporary test hack. */
845 snprintf (line
, DIM(line
)-1, "SCD PKAUTH %s", serialno
);
848 snprintf (line
, DIM(line
)-1, "SCD PKSIGN %s", serialno
);
849 line
[DIM(line
)-1] = 0;
850 rc
= assuan_transact (agent_ctx
, line
, membuf_data_cb
, &data
,
851 NULL
, NULL
, NULL
, NULL
);
854 xfree (get_membuf (&data
, &len
));
857 *r_buf
= get_membuf (&data
, r_buflen
);
863 /* Decrypt INDATA of length INDATALEN using the card identified by
864 SERIALNO. Return the plaintext in a nwly allocated buffer stored
865 at the address of R_BUF.
867 Note, we currently support only RSA or more exactly algorithms
868 taking one input data element. */
870 agent_scd_pkdecrypt (const char *serialno
,
871 const unsigned char *indata
, size_t indatalen
,
872 unsigned char **r_buf
, size_t *r_buflen
)
875 char *p
, line
[ASSUAN_LINELENGTH
];
884 /* FIXME: use secure memory where appropriate */
885 if (indatalen
*2 + 50 > DIM(line
))
886 return gpg_error (GPG_ERR_GENERAL
);
888 sprintf (line
, "SCD SETDATA ");
889 p
= line
+ strlen (line
);
890 for (i
=0; i
< indatalen
; i
++, p
+= 2 )
891 sprintf (p
, "%02X", indata
[i
]);
892 rc
= assuan_transact (agent_ctx
, line
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
);
896 init_membuf (&data
, 1024);
897 snprintf (line
, DIM(line
)-1, "SCD PKDECRYPT %s", serialno
);
898 line
[DIM(line
)-1] = 0;
899 rc
= assuan_transact (agent_ctx
, line
,
900 membuf_data_cb
, &data
,
901 NULL
, NULL
, NULL
, NULL
);
904 xfree (get_membuf (&data
, &len
));
907 *r_buf
= get_membuf (&data
, r_buflen
);
909 return gpg_error (GPG_ERR_ENOMEM
);
915 /* Change the PIN of an OpenPGP card or reset the retry counter.
916 CHVNO 1: Change the PIN
918 3: Change the admin PIN
919 101: Set a new PIN and reset the retry counter
921 SERIALNO is not used.
924 agent_scd_change_pin (int chvno
, const char *serialno
)
927 char line
[ASSUAN_LINELENGTH
];
928 const char *reset
= "";
938 snprintf (line
, DIM(line
)-1, "SCD PASSWD %s %d", reset
, chvno
);
939 line
[DIM(line
)-1] = 0;
940 rc
= assuan_transact (agent_ctx
, line
, NULL
, NULL
,
941 NULL
, NULL
, NULL
, NULL
);
946 /* Perform a CHECKPIN operation. SERIALNO should be the serial
947 number of the card - optionally followed by the fingerprint;
948 however the fingerprint is ignored here. */
950 agent_scd_checkpin (const char *serialno
)
953 char line
[ASSUAN_LINELENGTH
];
959 snprintf (line
, DIM(line
)-1, "SCD CHECKPIN %s", serialno
);
960 line
[DIM(line
)-1] = 0;
961 return assuan_transact (agent_ctx
, line
,
963 NULL
, NULL
, NULL
, NULL
);
967 /* Dummy function, only used by the gpg 1.4 implementation. */
969 agent_clear_pin_cache (const char *sn
)