1 /* call-agent.c - divert operations to the agent
2 * Copyright (C) 2001, 2002, 2003 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
37 #include "keydb.h" /* fixme: Move this to import.c */
38 #include "../common/membuf.h"
41 static ASSUAN_CONTEXT agent_ctx = NULL;
42 static int force_pipe_server = 0;
44 struct cipher_parm_s {
46 const char *ciphertext;
50 struct genkey_parm_s {
64 /* Try to connect to the agent via socket or fork it off and work by
65 pipes. Handle the server's initial greeting */
72 char *dft_display = NULL;
73 char *dft_ttyname = NULL;
74 char *dft_ttytype = NULL;
79 return 0; /* fixme: We need a context for each thread or serialize
80 the access to the agent (which is suitable given that
81 the agent is not MT */
83 infostr = force_pipe_server? NULL : getenv ("GPG_AGENT_INFO");
92 log_info (_("no running gpg-agent - starting one\n"));
96 gpg_error_t tmperr = gpg_error (gpg_err_code_from_errno (errno));
97 log_error ("error flushing pending output: %s\n", strerror (errno));
101 if (!opt.agent_program || !*opt.agent_program)
102 opt.agent_program = GNUPG_DEFAULT_AGENT;
103 if ( !(pgmname = strrchr (opt.agent_program, '/')))
104 pgmname = opt.agent_program;
109 argv[1] = "--server";
113 if (log_get_fd () != -1)
114 no_close_list[i++] = log_get_fd ();
115 no_close_list[i++] = fileno (stderr);
116 no_close_list[i] = -1;
118 /* connect to the agent and perform initial handshaking */
119 rc = assuan_pipe_connect (&ctx, opt.agent_program, (char**)argv,
127 infostr = xstrdup (infostr);
128 if ( !(p = strchr (infostr, ':')) || p == infostr)
130 log_error (_("malformed GPG_AGENT_INFO environment variable\n"));
132 force_pipe_server = 1;
133 return start_agent ();
137 while (*p && *p != ':')
139 prot = *p? atoi (p+1) : 0;
142 log_error (_("gpg-agent protocol version %d is not supported\n"),
145 force_pipe_server = 1;
146 return start_agent ();
149 rc = assuan_socket_connect (&ctx, infostr, pid);
151 if (rc == ASSUAN_Connect_Failed)
153 log_error (_("can't connect to the agent - trying fall back\n"));
154 force_pipe_server = 1;
155 return start_agent ();
161 log_error ("can't connect to the agent: %s\n", assuan_strerror (rc));
162 return gpg_error (GPG_ERR_NO_AGENT);
167 log_debug ("connection to agent established\n");
169 rc = assuan_transact (agent_ctx, "RESET", NULL, NULL, NULL, NULL, NULL, NULL);
171 return map_assuan_err (rc);
173 dft_display = getenv ("DISPLAY");
174 if (opt.display || dft_display)
177 if (asprintf (&optstr, "OPTION display=%s",
178 opt.display ? opt.display : dft_display) < 0)
179 return OUT_OF_CORE (errno);
180 rc = assuan_transact (agent_ctx, optstr, NULL, NULL, NULL, NULL, NULL,
184 return map_assuan_err (rc);
188 dft_ttyname = getenv ("GPG_TTY");
189 if ((!dft_ttyname || !*dft_ttyname) && ttyname (0))
190 dft_ttyname = ttyname (0);
192 if (opt.ttyname || dft_ttyname)
195 if (asprintf (&optstr, "OPTION ttyname=%s",
196 opt.ttyname ? opt.ttyname : dft_ttyname) < 0)
197 return OUT_OF_CORE (errno);
198 rc = assuan_transact (agent_ctx, optstr, NULL, NULL, NULL, NULL, NULL,
202 return map_assuan_err (rc);
204 dft_ttytype = getenv ("TERM");
205 if (opt.ttytype || (dft_ttyname && dft_ttytype))
208 if (asprintf (&optstr, "OPTION ttytype=%s",
209 opt.ttyname ? opt.ttytype : dft_ttytype) < 0)
210 return OUT_OF_CORE (errno);
211 rc = assuan_transact (agent_ctx, optstr, NULL, NULL, NULL, NULL, NULL,
215 return map_assuan_err (rc);
217 #if defined(HAVE_SETLOCALE) && defined(LC_CTYPE)
218 old_lc = setlocale (LC_CTYPE, NULL);
221 old_lc = strdup (old_lc);
223 return OUT_OF_CORE (errno);
225 dft_lc = setlocale (LC_CTYPE, "");
227 if (opt.lc_ctype || (dft_ttyname && dft_lc))
230 if (asprintf (&optstr, "OPTION lc-ctype=%s",
231 opt.lc_ctype ? opt.lc_ctype : dft_lc) < 0)
232 rc = OUT_OF_CORE (errno);
235 rc = assuan_transact (agent_ctx, optstr, NULL, NULL, NULL, NULL, NULL,
239 rc = map_assuan_err (rc);
242 #if defined(HAVE_SETLOCALE) && defined(LC_CTYPE)
245 setlocale (LC_CTYPE, old_lc);
251 #if defined(HAVE_SETLOCALE) && defined(LC_MESSAGES)
252 old_lc = setlocale (LC_MESSAGES, NULL);
255 old_lc = strdup (old_lc);
257 return OUT_OF_CORE (errno);
259 dft_lc = setlocale (LC_MESSAGES, "");
261 if (opt.lc_messages || (dft_ttyname && dft_lc))
264 if (asprintf (&optstr, "OPTION lc-messages=%s",
265 opt.lc_messages ? opt.lc_messages : dft_lc) < 0)
266 rc = OUT_OF_CORE (errno);
269 rc = assuan_transact (agent_ctx, optstr, NULL, NULL, NULL, NULL, NULL,
273 rc = map_assuan_err (rc);
276 #if defined(HAVE_SETLOCALE) && defined(LC_MESSAGES)
279 setlocale (LC_MESSAGES, old_lc);
289 membuf_data_cb (void *opaque, const void *buffer, size_t length)
291 membuf_t *data = opaque;
294 put_membuf (data, buffer, length);
301 /* Call the agent to do a sign operation using the key identified by
302 the hex string KEYGRIP. */
304 gpgsm_agent_pksign (const char *keygrip,
305 unsigned char *digest, size_t digestlen, int digestalgo,
306 char **r_buf, size_t *r_buflen )
309 char *p, line[ASSUAN_LINELENGTH];
318 if (digestlen*2 + 50 > DIM(line))
319 return gpg_error (GPG_ERR_GENERAL);
321 rc = assuan_transact (agent_ctx, "RESET", NULL, NULL, NULL, NULL, NULL, NULL);
323 return map_assuan_err (rc);
325 snprintf (line, DIM(line)-1, "SIGKEY %s", keygrip);
326 line[DIM(line)-1] = 0;
327 rc = assuan_transact (agent_ctx, line, NULL, NULL, NULL, NULL, NULL, NULL);
329 return map_assuan_err (rc);
331 sprintf (line, "SETHASH %d ", digestalgo);
332 p = line + strlen (line);
333 for (i=0; i < digestlen ; i++, p += 2 )
334 sprintf (p, "%02X", digest[i]);
335 rc = assuan_transact (agent_ctx, line, NULL, NULL, NULL, NULL, NULL, NULL);
337 return map_assuan_err (rc);
339 init_membuf (&data, 1024);
340 rc = assuan_transact (agent_ctx, "PKSIGN",
341 membuf_data_cb, &data, NULL, NULL, NULL, NULL);
344 xfree (get_membuf (&data, &len));
345 return map_assuan_err (rc);
347 *r_buf = get_membuf (&data, r_buflen);
349 if (!gcry_sexp_canon_len (*r_buf, *r_buflen, NULL, NULL))
351 xfree (*r_buf); *r_buf = NULL;
352 return gpg_error (GPG_ERR_INV_VALUE);
355 return *r_buf? 0 : OUT_OF_CORE (errno);
361 /* Handle a CIPHERTEXT inquiry. Note, we only send the data,
362 assuan_transact talkes care of flushing and writing the end */
364 inq_ciphertext_cb (void *opaque, const char *keyword)
366 struct cipher_parm_s *parm = opaque;
369 assuan_begin_confidential (parm->ctx);
370 rc = assuan_send_data (parm->ctx, parm->ciphertext, parm->ciphertextlen);
371 assuan_end_confidential (parm->ctx);
376 /* Call the agent to do a decrypt operation using the key identified by
377 the hex string KEYGRIP. */
379 gpgsm_agent_pkdecrypt (const char *keygrip,
380 ksba_const_sexp_t ciphertext,
381 char **r_buf, size_t *r_buflen )
384 char line[ASSUAN_LINELENGTH];
386 struct cipher_parm_s cipher_parm;
389 size_t ciphertextlen;
391 if (!keygrip || strlen(keygrip) != 40 || !ciphertext || !r_buf || !r_buflen)
392 return gpg_error (GPG_ERR_INV_VALUE);
395 ciphertextlen = gcry_sexp_canon_len (ciphertext, 0, NULL, NULL);
397 return gpg_error (GPG_ERR_INV_VALUE);
403 rc = assuan_transact (agent_ctx, "RESET", NULL, NULL, NULL, NULL, NULL, NULL);
405 return map_assuan_err (rc);
407 assert ( DIM(line) >= 50 );
408 snprintf (line, DIM(line)-1, "SETKEY %s", keygrip);
409 line[DIM(line)-1] = 0;
410 rc = assuan_transact (agent_ctx, line, NULL, NULL, NULL, NULL, NULL, NULL);
412 return map_assuan_err (rc);
414 init_membuf (&data, 1024);
415 cipher_parm.ctx = agent_ctx;
416 cipher_parm.ciphertext = ciphertext;
417 cipher_parm.ciphertextlen = ciphertextlen;
418 rc = assuan_transact (agent_ctx, "PKDECRYPT",
419 membuf_data_cb, &data,
420 inq_ciphertext_cb, &cipher_parm, NULL, NULL);
423 xfree (get_membuf (&data, &len));
424 return map_assuan_err (rc);
427 put_membuf (&data, "", 1); /* make sure it is 0 terminated */
428 buf = get_membuf (&data, &len);
430 return gpg_error (GPG_ERR_ENOMEM);
431 /* FIXME: We would better a return a full S-exp and not just a part */
433 len--; /* remove the terminating 0 */
434 n = strtoul (buf, &endp, 10);
435 if (!n || *endp != ':')
436 return gpg_error (GPG_ERR_INV_SEXP);
438 if (endp-buf+n > len)
439 return gpg_error (GPG_ERR_INV_SEXP); /* oops len does not
441 memmove (buf, endp, n);
451 /* Handle a KEYPARMS inquiry. Note, we only send the data,
452 assuan_transact takes care of flushing and writing the end */
454 inq_genkey_parms (void *opaque, const char *keyword)
456 struct genkey_parm_s *parm = opaque;
459 rc = assuan_send_data (parm->ctx, parm->sexp, parm->sexplen);
465 /* Call the agent to generate a newkey */
467 gpgsm_agent_genkey (ksba_const_sexp_t keyparms, ksba_sexp_t *r_pubkey)
470 struct genkey_parm_s gk_parm;
480 rc = assuan_transact (agent_ctx, "RESET", NULL, NULL, NULL, NULL, NULL, NULL);
482 return map_assuan_err (rc);
484 init_membuf (&data, 1024);
485 gk_parm.ctx = agent_ctx;
486 gk_parm.sexp = keyparms;
487 gk_parm.sexplen = gcry_sexp_canon_len (keyparms, 0, NULL, NULL);
488 if (!gk_parm.sexplen)
489 return gpg_error (GPG_ERR_INV_VALUE);
490 rc = assuan_transact (agent_ctx, "GENKEY",
491 membuf_data_cb, &data,
492 inq_genkey_parms, &gk_parm, NULL, NULL);
495 xfree (get_membuf (&data, &len));
496 return map_assuan_err (rc);
498 buf = get_membuf (&data, &len);
500 return gpg_error (GPG_ERR_ENOMEM);
501 if (!gcry_sexp_canon_len (buf, len, NULL, NULL))
504 return gpg_error (GPG_ERR_INV_SEXP);
511 /* Ask the agent whether the certificate is in the list of trusted
514 gpgsm_agent_istrusted (ksba_cert_t cert)
518 char line[ASSUAN_LINELENGTH];
524 fpr = gpgsm_get_fingerprint_hexstring (cert, GCRY_MD_SHA1);
527 log_error ("error getting the fingerprint\n");
528 return gpg_error (GPG_ERR_GENERAL);
531 snprintf (line, DIM(line)-1, "ISTRUSTED %s", fpr);
532 line[DIM(line)-1] = 0;
535 rc = assuan_transact (agent_ctx, line, NULL, NULL, NULL, NULL, NULL, NULL);
536 return map_assuan_err (rc);
539 /* Ask the agent to mark CERT as a trusted Root-CA one */
541 gpgsm_agent_marktrusted (ksba_cert_t cert)
545 char line[ASSUAN_LINELENGTH];
551 fpr = gpgsm_get_fingerprint_hexstring (cert, GCRY_MD_SHA1);
554 log_error ("error getting the fingerprint\n");
555 return gpg_error (GPG_ERR_GENERAL);
558 dn = ksba_cert_get_issuer (cert, 0);
562 return gpg_error (GPG_ERR_GENERAL);
564 snprintf (line, DIM(line)-1, "MARKTRUSTED %s S %s", fpr, dn);
565 line[DIM(line)-1] = 0;
569 rc = assuan_transact (agent_ctx, line, NULL, NULL, NULL, NULL, NULL, NULL);
570 return map_assuan_err (rc);
575 /* Ask the agent whether the a corresponding secret key is available
576 for the given keygrip */
578 gpgsm_agent_havekey (const char *hexkeygrip)
581 char line[ASSUAN_LINELENGTH];
587 if (!hexkeygrip || strlen (hexkeygrip) != 40)
588 return gpg_error (GPG_ERR_INV_VALUE);
590 snprintf (line, DIM(line)-1, "HAVEKEY %s", hexkeygrip);
591 line[DIM(line)-1] = 0;
593 rc = assuan_transact (agent_ctx, line, NULL, NULL, NULL, NULL, NULL, NULL);
594 return map_assuan_err (rc);
599 learn_cb (void *opaque, const void *buffer, size_t length)
601 struct learn_parm_s *parm = opaque;
612 put_membuf (parm->data, buffer, length);
615 /* END encountered - process what we have */
616 buf = get_membuf (parm->data, &len);
619 parm->error = gpg_error (GPG_ERR_ENOMEM);
624 /* FIXME: this should go into import.c */
625 rc = ksba_cert_new (&cert);
631 rc = ksba_cert_init_from_mem (cert, buf, len);
634 log_error ("failed to parse a certificate: %s\n", gpg_strerror (rc));
635 ksba_cert_release (cert);
640 rc = gpgsm_basic_cert_check (cert);
641 if (gpg_err_code (rc) == GPG_ERR_MISSING_CERT)
642 { /* For later use we store it in the ephemeral database. */
643 log_info ("issuer certificate missing - storing as ephemeral\n");
644 keydb_store_cert (cert, 1, NULL);
647 log_error ("invalid certificate: %s\n", gpg_strerror (rc));
652 if (!keydb_store_cert (cert, 0, &existed))
654 if (opt.verbose > 1 && existed)
655 log_info ("certificate already in DB\n");
656 else if (opt.verbose && !existed)
657 log_info ("certificate imported\n");
661 ksba_cert_release (cert);
662 init_membuf (parm->data, 4096);
666 /* Call the agent to learn about a smartcard */
671 struct learn_parm_s learn_parm;
679 init_membuf (&data, 4096);
680 learn_parm.error = 0;
681 learn_parm.ctx = agent_ctx;
682 learn_parm.data = &data;
683 rc = assuan_transact (agent_ctx, "LEARN --send",
684 learn_cb, &learn_parm,
685 NULL, NULL, NULL, NULL);
686 xfree (get_membuf (&data, &len));
688 return map_assuan_err (rc);
689 return learn_parm.error;
693 /* Ask the agent to change the passphrase of the key identified by HEXKEYGRIP. */
695 gpgsm_agent_passwd (const char *hexkeygrip)
698 char line[ASSUAN_LINELENGTH];
704 if (!hexkeygrip || strlen (hexkeygrip) != 40)
705 return gpg_error (GPG_ERR_INV_VALUE);
707 snprintf (line, DIM(line)-1, "PASSWD %s", hexkeygrip);
708 line[DIM(line)-1] = 0;
710 rc = assuan_transact (agent_ctx, line, NULL, NULL, NULL, NULL, NULL, NULL);
711 return map_assuan_err (rc);