1 /* call-agent.c - Divert GPGSM operations to the agent
2 * Copyright (C) 2001, 2002, 2003, 2005, 2007,
3 * 2008, 2009, 2010 Free Software Foundation, Inc.
5 * This file is part of GnuPG.
7 * GnuPG is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 3 of the License, or
10 * (at your option) any later version.
12 * GnuPG is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, see <http://www.gnu.org/licenses/>.
38 #include "keydb.h" /* fixme: Move this to import.c */
40 #include "shareddefs.h"
41 #include "passphrase.h"
44 static assuan_context_t agent_ctx = NULL;
51 const unsigned char *ciphertext;
59 const unsigned char *sexp;
71 struct import_key_parm_s
79 struct default_inq_parm_s
86 /* Print a warning if the server's version number is less than our
87 version number. Returns an error code on a connection problem. */
89 warn_version_mismatch (ctrl_t ctrl, assuan_context_t ctx,
90 const char *servername, int mode)
94 const char *myversion = strusage (13);
96 err = get_assuan_server_version (ctx, mode, &serverversion);
98 log_error (_("error getting version from '%s': %s\n"),
99 servername, gpg_strerror (err));
100 else if (!compare_version_strings (serverversion, myversion))
104 warn = xtryasprintf (_("server '%s' is older than us (%s < %s)"),
105 servername, serverversion, myversion);
107 err = gpg_error_from_syserror ();
110 log_info (_("WARNING: %s\n"), warn);
111 gpgsm_status2 (ctrl, STATUS_WARNING, "server_version_mismatch 0",
116 xfree (serverversion);
121 /* Try to connect to the agent via socket or fork it off and work by
122 pipes. Handle the server's initial greeting */
124 start_agent (ctrl_t ctrl)
129 rc = 0; /* fixme: We need a context for each thread or
130 serialize the access to the agent (which is
131 suitable given that the agent is not MT. */
134 rc = start_new_gpg_agent (&agent_ctx,
135 GPG_ERR_SOURCE_DEFAULT,
137 opt.lc_ctype, opt.lc_messages,
139 opt.autostart, opt.verbose, DBG_IPC,
140 gpgsm_status2, ctrl);
142 if (!opt.autostart && gpg_err_code (rc) == GPG_ERR_NO_AGENT)
149 log_info (_("no gpg-agent running in this session\n"));
152 else if (!rc && !(rc = warn_version_mismatch (ctrl, agent_ctx,
155 /* Tell the agent that we support Pinentry notifications. No
156 error checking so that it will work also with older
158 assuan_transact (agent_ctx, "OPTION allow-pinentry-notify",
159 NULL, NULL, NULL, NULL, NULL, NULL);
161 /* Pass on the pinentry mode. */
162 if (opt.pinentry_mode)
164 char *tmp = xasprintf ("OPTION pinentry-mode=%s",
165 str_pinentry_mode (opt.pinentry_mode));
166 rc = assuan_transact (agent_ctx, tmp,
167 NULL, NULL, NULL, NULL, NULL, NULL);
170 log_error ("setting pinentry mode '%s' failed: %s\n",
171 str_pinentry_mode (opt.pinentry_mode),
177 if (!ctrl->agent_seen)
179 ctrl->agent_seen = 1;
180 audit_log_ok (ctrl->audit, AUDIT_AGENT_READY, rc);
186 /* This is the default inquiry callback. It mainly handles the
187 Pinentry notifications. */
189 default_inq_cb (void *opaque, const char *line)
192 struct default_inq_parm_s *parm = opaque;
193 ctrl_t ctrl = parm->ctrl;
195 if (has_leading_keyword (line, "PINENTRY_LAUNCHED"))
197 err = gpgsm_proxy_pinentry_notify (ctrl, line);
199 log_error (_("failed to proxy %s inquiry to client\n"),
200 "PINENTRY_LAUNCHED");
201 /* We do not pass errors to avoid breaking other code. */
203 else if ((has_leading_keyword (line, "PASSPHRASE")
204 || has_leading_keyword (line, "NEW_PASSPHRASE"))
205 && opt.pinentry_mode == PINENTRY_MODE_LOOPBACK
206 && have_static_passphrase ())
208 const char *s = get_static_passphrase ();
209 err = assuan_send_data (parm->ctx, s, strlen (s));
212 log_error ("ignoring gpg-agent inquiry '%s'\n", line);
220 /* Call the agent to do a sign operation using the key identified by
221 the hex string KEYGRIP. */
223 gpgsm_agent_pksign (ctrl_t ctrl, const char *keygrip, const char *desc,
224 unsigned char *digest, size_t digestlen, int digestalgo,
225 unsigned char **r_buf, size_t *r_buflen )
228 char *p, line[ASSUAN_LINELENGTH];
231 struct default_inq_parm_s inq_parm = { ctrl, agent_ctx };
234 rc = start_agent (ctrl);
238 if (digestlen*2 + 50 > DIM(line))
239 return gpg_error (GPG_ERR_GENERAL);
241 rc = assuan_transact (agent_ctx, "RESET", NULL, NULL, NULL, NULL, NULL, NULL);
245 snprintf (line, DIM(line)-1, "SIGKEY %s", keygrip);
246 line[DIM(line)-1] = 0;
247 rc = assuan_transact (agent_ctx, line, NULL, NULL, NULL, NULL, NULL, NULL);
253 snprintf (line, DIM(line)-1, "SETKEYDESC %s", desc);
254 line[DIM(line)-1] = 0;
255 rc = assuan_transact (agent_ctx, line,
256 NULL, NULL, NULL, NULL, NULL, NULL);
261 sprintf (line, "SETHASH %d ", digestalgo);
262 p = line + strlen (line);
263 for (i=0; i < digestlen ; i++, p += 2 )
264 sprintf (p, "%02X", digest[i]);
265 rc = assuan_transact (agent_ctx, line, NULL, NULL, NULL, NULL, NULL, NULL);
269 init_membuf (&data, 1024);
270 rc = assuan_transact (agent_ctx, "PKSIGN",
271 put_membuf_cb, &data, default_inq_cb, &inq_parm,
275 xfree (get_membuf (&data, &len));
278 *r_buf = get_membuf (&data, r_buflen);
280 if (!gcry_sexp_canon_len (*r_buf, *r_buflen, NULL, NULL))
282 xfree (*r_buf); *r_buf = NULL;
283 return gpg_error (GPG_ERR_INV_VALUE);
286 return *r_buf? 0 : out_of_core ();
290 /* Call the scdaemon to do a sign operation using the key identified by
291 the hex string KEYID. */
293 gpgsm_scd_pksign (ctrl_t ctrl, const char *keyid, const char *desc,
294 unsigned char *digest, size_t digestlen, int digestalgo,
295 unsigned char **r_buf, size_t *r_buflen )
298 char *p, line[ASSUAN_LINELENGTH];
302 unsigned char *sigbuf;
304 struct default_inq_parm_s inq_parm = { ctrl, agent_ctx };
312 case GCRY_MD_SHA1: hashopt = "--hash=sha1"; break;
313 case GCRY_MD_RMD160:hashopt = "--hash=rmd160"; break;
314 case GCRY_MD_MD5: hashopt = "--hash=md5"; break;
315 case GCRY_MD_SHA256:hashopt = "--hash=sha256"; break;
317 return gpg_error (GPG_ERR_DIGEST_ALGO);
320 rc = start_agent (ctrl);
324 if (digestlen*2 + 50 > DIM(line))
325 return gpg_error (GPG_ERR_GENERAL);
327 p = stpcpy (line, "SCD SETDATA " );
328 for (i=0; i < digestlen ; i++, p += 2 )
329 sprintf (p, "%02X", digest[i]);
330 rc = assuan_transact (agent_ctx, line, NULL, NULL, NULL, NULL, NULL, NULL);
334 init_membuf (&data, 1024);
336 snprintf (line, DIM(line)-1, "SCD PKSIGN %s %s", hashopt, keyid);
337 line[DIM(line)-1] = 0;
338 rc = assuan_transact (agent_ctx, line,
339 put_membuf_cb, &data, default_inq_cb, &inq_parm,
343 xfree (get_membuf (&data, &len));
346 sigbuf = get_membuf (&data, &sigbuflen);
348 /* Create an S-expression from it which is formatted like this:
349 "(7:sig-val(3:rsa(1:sSIGBUFLEN:SIGBUF)))" Fixme: If a card ever
350 creates non-RSA keys we need to change things. */
351 *r_buflen = 21 + 11 + sigbuflen + 4;
352 p = xtrymalloc (*r_buflen);
353 *r_buf = (unsigned char*)p;
359 p = stpcpy (p, "(7:sig-val(3:rsa(1:s" );
360 sprintf (p, "%u:", (unsigned int)sigbuflen);
362 memcpy (p, sigbuf, sigbuflen);
367 assert (gcry_sexp_canon_len (*r_buf, *r_buflen, NULL, NULL));
374 /* Handle a CIPHERTEXT inquiry. Note, we only send the data,
375 assuan_transact takes care of flushing and writing the end */
377 inq_ciphertext_cb (void *opaque, const char *line)
379 struct cipher_parm_s *parm = opaque;
382 if (has_leading_keyword (line, "CIPHERTEXT"))
384 assuan_begin_confidential (parm->ctx);
385 rc = assuan_send_data (parm->ctx, parm->ciphertext, parm->ciphertextlen);
386 assuan_end_confidential (parm->ctx);
390 struct default_inq_parm_s inq_parm = { parm->ctrl, parm->ctx };
391 rc = default_inq_cb (&inq_parm, line);
398 /* Call the agent to do a decrypt operation using the key identified by
399 the hex string KEYGRIP. */
401 gpgsm_agent_pkdecrypt (ctrl_t ctrl, const char *keygrip, const char *desc,
402 ksba_const_sexp_t ciphertext,
403 char **r_buf, size_t *r_buflen )
406 char line[ASSUAN_LINELENGTH];
408 struct cipher_parm_s cipher_parm;
410 char *p, *buf, *endp;
411 size_t ciphertextlen;
413 if (!keygrip || strlen(keygrip) != 40 || !ciphertext || !r_buf || !r_buflen)
414 return gpg_error (GPG_ERR_INV_VALUE);
417 ciphertextlen = gcry_sexp_canon_len (ciphertext, 0, NULL, NULL);
419 return gpg_error (GPG_ERR_INV_VALUE);
421 rc = start_agent (ctrl);
425 rc = assuan_transact (agent_ctx, "RESET", NULL, NULL, NULL, NULL, NULL, NULL);
429 assert ( DIM(line) >= 50 );
430 snprintf (line, DIM(line)-1, "SETKEY %s", keygrip);
431 line[DIM(line)-1] = 0;
432 rc = assuan_transact (agent_ctx, line, NULL, NULL, NULL, NULL, NULL, NULL);
438 snprintf (line, DIM(line)-1, "SETKEYDESC %s", desc);
439 line[DIM(line)-1] = 0;
440 rc = assuan_transact (agent_ctx, line,
441 NULL, NULL, NULL, NULL, NULL, NULL);
446 init_membuf (&data, 1024);
447 cipher_parm.ctrl = ctrl;
448 cipher_parm.ctx = agent_ctx;
449 cipher_parm.ciphertext = ciphertext;
450 cipher_parm.ciphertextlen = ciphertextlen;
451 rc = assuan_transact (agent_ctx, "PKDECRYPT",
452 put_membuf_cb, &data,
453 inq_ciphertext_cb, &cipher_parm, NULL, NULL);
456 xfree (get_membuf (&data, &len));
460 put_membuf (&data, "", 1); /* Make sure it is 0 terminated. */
461 buf = get_membuf (&data, &len);
463 return gpg_error (GPG_ERR_ENOMEM);
464 assert (len); /* (we forced Nul termination.) */
468 if (len < 13 || memcmp (buf, "(5:value", 8) ) /* "(5:valueN:D)\0" */
469 return gpg_error (GPG_ERR_INV_SEXP);
470 len -= 11; /* Count only the data of the second part. */
471 p = buf + 8; /* Skip leading parenthesis and the value tag. */
475 /* For compatibility with older gpg-agents handle the old style
476 incomplete S-exps. */
477 len--; /* Do not count the Nul. */
481 n = strtoul (p, &endp, 10);
482 if (!n || *endp != ':')
483 return gpg_error (GPG_ERR_INV_SEXP);
486 return gpg_error (GPG_ERR_INV_SEXP); /* Oops: Inconsistent S-Exp. */
488 memmove (buf, endp, n);
499 /* Handle a KEYPARMS inquiry. Note, we only send the data,
500 assuan_transact takes care of flushing and writing the end */
502 inq_genkey_parms (void *opaque, const char *line)
504 struct genkey_parm_s *parm = opaque;
507 if (has_leading_keyword (line, "KEYPARAM"))
509 rc = assuan_send_data (parm->ctx, parm->sexp, parm->sexplen);
513 struct default_inq_parm_s inq_parm = { parm->ctrl, parm->ctx };
514 rc = default_inq_cb (&inq_parm, line);
522 /* Call the agent to generate a newkey */
524 gpgsm_agent_genkey (ctrl_t ctrl,
525 ksba_const_sexp_t keyparms, ksba_sexp_t *r_pubkey)
528 struct genkey_parm_s gk_parm;
534 rc = start_agent (ctrl);
538 rc = assuan_transact (agent_ctx, "RESET", NULL, NULL, NULL, NULL, NULL, NULL);
542 init_membuf (&data, 1024);
544 gk_parm.ctx = agent_ctx;
545 gk_parm.sexp = keyparms;
546 gk_parm.sexplen = gcry_sexp_canon_len (keyparms, 0, NULL, NULL);
547 if (!gk_parm.sexplen)
548 return gpg_error (GPG_ERR_INV_VALUE);
549 rc = assuan_transact (agent_ctx, "GENKEY",
550 put_membuf_cb, &data,
551 inq_genkey_parms, &gk_parm, NULL, NULL);
554 xfree (get_membuf (&data, &len));
557 buf = get_membuf (&data, &len);
559 return gpg_error (GPG_ERR_ENOMEM);
560 if (!gcry_sexp_canon_len (buf, len, NULL, NULL))
563 return gpg_error (GPG_ERR_INV_SEXP);
570 /* Call the agent to read the public key part for a given keygrip. If
571 FROMCARD is true, the key is directly read from the current
572 smartcard. In this case HEXKEYGRIP should be the keyID
575 gpgsm_agent_readkey (ctrl_t ctrl, int fromcard, const char *hexkeygrip,
576 ksba_sexp_t *r_pubkey)
582 char line[ASSUAN_LINELENGTH];
583 struct default_inq_parm_s inq_parm = { ctrl, agent_ctx };
586 rc = start_agent (ctrl);
590 rc = assuan_transact (agent_ctx, "RESET",NULL, NULL, NULL, NULL, NULL, NULL);
594 snprintf (line, DIM(line)-1, "%sREADKEY %s",
595 fromcard? "SCD ":"", hexkeygrip);
596 line[DIM(line)-1] = 0;
598 init_membuf (&data, 1024);
599 rc = assuan_transact (agent_ctx, line,
600 put_membuf_cb, &data,
601 default_inq_cb, &inq_parm, NULL, NULL);
604 xfree (get_membuf (&data, &len));
607 buf = get_membuf (&data, &len);
609 return gpg_error (GPG_ERR_ENOMEM);
610 if (!gcry_sexp_canon_len (buf, len, NULL, NULL))
613 return gpg_error (GPG_ERR_INV_SEXP);
621 /* Take the serial number from LINE and return it verbatim in a newly
622 allocated string. We make sure that only hex characters are
625 store_serialno (const char *line)
630 for (s=line; hexdigitp (s); s++)
632 p = xtrymalloc (s + 1 - line);
635 memcpy (p, line, s-line);
642 /* Callback for the gpgsm_agent_serialno function. */
644 scd_serialno_status_cb (void *opaque, const char *line)
646 char **r_serialno = opaque;
647 const char *keyword = line;
650 for (keywordlen=0; *line && !spacep (line); line++, keywordlen++)
652 while (spacep (line))
655 if (keywordlen == 8 && !memcmp (keyword, "SERIALNO", keywordlen))
658 *r_serialno = store_serialno (line);
665 /* Call the agent to read the serial number of the current card. */
667 gpgsm_agent_scd_serialno (ctrl_t ctrl, char **r_serialno)
670 char *serialno = NULL;
671 struct default_inq_parm_s inq_parm = { ctrl, agent_ctx };
674 rc = start_agent (ctrl);
678 rc = assuan_transact (agent_ctx, "SCD SERIALNO",
680 default_inq_cb, &inq_parm,
681 scd_serialno_status_cb, &serialno);
682 if (!rc && !serialno)
683 rc = gpg_error (GPG_ERR_INTERNAL);
689 *r_serialno = serialno;
695 /* Callback for the gpgsm_agent_serialno function. */
697 scd_keypairinfo_status_cb (void *opaque, const char *line)
699 strlist_t *listaddr = opaque;
700 const char *keyword = line;
705 for (keywordlen=0; *line && !spacep (line); line++, keywordlen++)
707 while (spacep (line))
710 if (keywordlen == 11 && !memcmp (keyword, "KEYPAIRINFO", keywordlen))
712 sl = append_to_strlist (listaddr, line);
714 /* Make sure that we only have two tokes so that future
715 extensions of the format won't change the format expected by
717 while (*p && !spacep (p))
723 while (*p && !spacep (p))
733 /* Call the agent to read the keypairinfo lines of the current card.
734 The list is returned as a string made up of the keygrip, a space
737 gpgsm_agent_scd_keypairinfo (ctrl_t ctrl, strlist_t *r_list)
740 strlist_t list = NULL;
741 struct default_inq_parm_s inq_parm = { ctrl, agent_ctx };
744 rc = start_agent (ctrl);
748 rc = assuan_transact (agent_ctx, "SCD LEARN --force",
750 default_inq_cb, &inq_parm,
751 scd_keypairinfo_status_cb, &list);
753 rc = gpg_error (GPG_ERR_NO_DATA);
766 istrusted_status_cb (void *opaque, const char *line)
768 struct rootca_flags_s *flags = opaque;
771 if ((s = has_leading_keyword (line, "TRUSTLISTFLAG")))
774 if (has_leading_keyword (line, "relax"))
776 else if (has_leading_keyword (line, "cm"))
777 flags->chain_model = 1;
784 /* Ask the agent whether the certificate is in the list of trusted
785 keys. The certificate is either specified by the CERT object or by
786 the fingerprint HEXFPR. ROOTCA_FLAGS is guaranteed to be cleared
789 gpgsm_agent_istrusted (ctrl_t ctrl, ksba_cert_t cert, const char *hexfpr,
790 struct rootca_flags_s *rootca_flags)
793 char line[ASSUAN_LINELENGTH];
795 memset (rootca_flags, 0, sizeof *rootca_flags);
798 return gpg_error (GPG_ERR_INV_ARG);
800 rc = start_agent (ctrl);
806 snprintf (line, DIM(line)-1, "ISTRUSTED %s", hexfpr);
807 line[DIM(line)-1] = 0;
813 fpr = gpgsm_get_fingerprint_hexstring (cert, GCRY_MD_SHA1);
816 log_error ("error getting the fingerprint\n");
817 return gpg_error (GPG_ERR_GENERAL);
820 snprintf (line, DIM(line)-1, "ISTRUSTED %s", fpr);
821 line[DIM(line)-1] = 0;
825 rc = assuan_transact (agent_ctx, line, NULL, NULL, NULL, NULL,
826 istrusted_status_cb, rootca_flags);
828 rootca_flags->valid = 1;
832 /* Ask the agent to mark CERT as a trusted Root-CA one */
834 gpgsm_agent_marktrusted (ctrl_t ctrl, ksba_cert_t cert)
837 char *fpr, *dn, *dnfmt;
838 char line[ASSUAN_LINELENGTH];
839 struct default_inq_parm_s inq_parm = { ctrl, agent_ctx };
841 rc = start_agent (ctrl);
845 fpr = gpgsm_get_fingerprint_hexstring (cert, GCRY_MD_SHA1);
848 log_error ("error getting the fingerprint\n");
849 return gpg_error (GPG_ERR_GENERAL);
852 dn = ksba_cert_get_issuer (cert, 0);
856 return gpg_error (GPG_ERR_GENERAL);
858 dnfmt = gpgsm_format_name2 (dn, 0);
861 return gpg_error_from_syserror ();
862 snprintf (line, DIM(line)-1, "MARKTRUSTED %s S %s", fpr, dnfmt);
863 line[DIM(line)-1] = 0;
867 rc = assuan_transact (agent_ctx, line, NULL, NULL,
868 default_inq_cb, &inq_parm, NULL, NULL);
874 /* Ask the agent whether the a corresponding secret key is available
875 for the given keygrip */
877 gpgsm_agent_havekey (ctrl_t ctrl, const char *hexkeygrip)
880 char line[ASSUAN_LINELENGTH];
882 rc = start_agent (ctrl);
886 if (!hexkeygrip || strlen (hexkeygrip) != 40)
887 return gpg_error (GPG_ERR_INV_VALUE);
889 snprintf (line, DIM(line)-1, "HAVEKEY %s", hexkeygrip);
890 line[DIM(line)-1] = 0;
892 rc = assuan_transact (agent_ctx, line, NULL, NULL, NULL, NULL, NULL, NULL);
898 learn_status_cb (void *opaque, const char *line)
900 struct learn_parm_s *parm = opaque;
903 /* Pass progress data to the caller. */
904 if ((s = has_leading_keyword (line, "PROGRESS")))
909 if (gpgsm_status (parm->ctrl, STATUS_PROGRESS, line))
910 return gpg_error (GPG_ERR_ASS_CANCELED);
917 learn_cb (void *opaque, const void *buffer, size_t length)
919 struct learn_parm_s *parm = opaque;
930 put_membuf (parm->data, buffer, length);
933 /* END encountered - process what we have */
934 buf = get_membuf (parm->data, &len);
937 parm->error = gpg_error (GPG_ERR_ENOMEM);
941 if (gpgsm_status (parm->ctrl, STATUS_PROGRESS, "learncard C 0 0"))
942 return gpg_error (GPG_ERR_ASS_CANCELED);
944 /* FIXME: this should go into import.c */
945 rc = ksba_cert_new (&cert);
951 rc = ksba_cert_init_from_mem (cert, buf, len);
954 log_error ("failed to parse a certificate: %s\n", gpg_strerror (rc));
955 ksba_cert_release (cert);
960 /* We do not store a certifciate with missing issuers as ephemeral
961 because we can assume that the --learn-card command has been used
963 rc = gpgsm_basic_cert_check (parm->ctrl, cert);
964 if (rc && gpg_err_code (rc) != GPG_ERR_MISSING_CERT
965 && gpg_err_code (rc) != GPG_ERR_MISSING_ISSUER_CERT)
966 log_error ("invalid certificate: %s\n", gpg_strerror (rc));
971 if (!keydb_store_cert (cert, 0, &existed))
973 if (opt.verbose > 1 && existed)
974 log_info ("certificate already in DB\n");
975 else if (opt.verbose && !existed)
976 log_info ("certificate imported\n");
980 ksba_cert_release (cert);
981 init_membuf (parm->data, 4096);
985 /* Call the agent to learn about a smartcard */
987 gpgsm_agent_learn (ctrl_t ctrl)
990 struct learn_parm_s learn_parm;
994 rc = start_agent (ctrl);
998 rc = warn_version_mismatch (ctrl, agent_ctx, SCDAEMON_NAME, 2);
1002 init_membuf (&data, 4096);
1003 learn_parm.error = 0;
1004 learn_parm.ctrl = ctrl;
1005 learn_parm.ctx = agent_ctx;
1006 learn_parm.data = &data;
1007 rc = assuan_transact (agent_ctx, "LEARN --send",
1008 learn_cb, &learn_parm,
1010 learn_status_cb, &learn_parm);
1011 xfree (get_membuf (&data, &len));
1014 return learn_parm.error;
1018 /* Ask the agent to change the passphrase of the key identified by
1019 HEXKEYGRIP. If DESC is not NULL, display instead of the default
1020 description message. */
1022 gpgsm_agent_passwd (ctrl_t ctrl, const char *hexkeygrip, const char *desc)
1025 char line[ASSUAN_LINELENGTH];
1026 struct default_inq_parm_s inq_parm = { ctrl, agent_ctx };
1028 rc = start_agent (ctrl);
1032 if (!hexkeygrip || strlen (hexkeygrip) != 40)
1033 return gpg_error (GPG_ERR_INV_VALUE);
1037 snprintf (line, DIM(line)-1, "SETKEYDESC %s", desc);
1038 line[DIM(line)-1] = 0;
1039 rc = assuan_transact (agent_ctx, line,
1040 NULL, NULL, NULL, NULL, NULL, NULL);
1045 snprintf (line, DIM(line)-1, "PASSWD %s", hexkeygrip);
1046 line[DIM(line)-1] = 0;
1048 rc = assuan_transact (agent_ctx, line, NULL, NULL,
1049 default_inq_cb, &inq_parm, NULL, NULL);
1055 /* Ask the agent to pop up a confirmation dialog with the text DESC
1056 and an okay and cancel button. */
1058 gpgsm_agent_get_confirmation (ctrl_t ctrl, const char *desc)
1061 char line[ASSUAN_LINELENGTH];
1062 struct default_inq_parm_s inq_parm = { ctrl, agent_ctx };
1064 rc = start_agent (ctrl);
1068 snprintf (line, DIM(line)-1, "GET_CONFIRMATION %s", desc);
1069 line[DIM(line)-1] = 0;
1071 rc = assuan_transact (agent_ctx, line, NULL, NULL,
1072 default_inq_cb, &inq_parm, NULL, NULL);
1078 /* Return 0 if the agent is alive. This is useful to make sure that
1079 an agent has been started. */
1081 gpgsm_agent_send_nop (ctrl_t ctrl)
1085 rc = start_agent (ctrl);
1087 rc = assuan_transact (agent_ctx, "NOP",
1088 NULL, NULL, NULL, NULL, NULL, NULL);
1095 keyinfo_status_cb (void *opaque, const char *line)
1097 char **serialno = opaque;
1100 if ((s = has_leading_keyword (line, "KEYINFO")) && !*serialno)
1102 s = strchr (s, ' ');
1103 if (s && s[1] == 'T' && s[2] == ' ' && s[3])
1106 s2 = strchr (s, ' ');
1109 *serialno = xtrymalloc ((s2 - s)+1);
1112 memcpy (*serialno, s, s2 - s);
1113 (*serialno)[s2 - s] = 0;
1121 /* Return the serial number for a secret key. If the returned serial
1122 number is NULL, the key is not stored on a smartcard. Caller needs
1123 to free R_SERIALNO. */
1125 gpgsm_agent_keyinfo (ctrl_t ctrl, const char *hexkeygrip, char **r_serialno)
1128 char line[ASSUAN_LINELENGTH];
1129 char *serialno = NULL;
1133 err = start_agent (ctrl);
1137 if (!hexkeygrip || strlen (hexkeygrip) != 40)
1138 return gpg_error (GPG_ERR_INV_VALUE);
1140 snprintf (line, DIM(line)-1, "KEYINFO %s", hexkeygrip);
1141 line[DIM(line)-1] = 0;
1143 err = assuan_transact (agent_ctx, line, NULL, NULL, NULL, NULL,
1144 keyinfo_status_cb, &serialno);
1145 if (!err && serialno)
1147 /* Sanity check for bad characters. */
1148 if (strpbrk (serialno, ":\n\r"))
1149 err = GPG_ERR_INV_VALUE;
1154 *r_serialno = serialno;
1160 /* Ask for the passphrase (this is used for pkcs#12 import/export. On
1161 success the caller needs to free the string stored at R_PASSPHRASE.
1162 On error NULL will be stored at R_PASSPHRASE and an appropriate
1163 error code returned. If REPEAT is true the agent tries to get a
1164 new passphrase (i.e. asks the user to confirm it). */
1166 gpgsm_agent_ask_passphrase (ctrl_t ctrl, const char *desc_msg, int repeat,
1167 char **r_passphrase)
1170 char line[ASSUAN_LINELENGTH];
1173 struct default_inq_parm_s inq_parm = { ctrl, agent_ctx };
1175 *r_passphrase = NULL;
1177 err = start_agent (ctrl);
1181 if (desc_msg && *desc_msg && !(arg4 = percent_plus_escape (desc_msg)))
1182 return gpg_error_from_syserror ();
1184 snprintf (line, DIM(line)-1, "GET_PASSPHRASE --data%s -- X X X %s",
1185 repeat? " --repeat=1 --check --qualitybar":"",
1189 init_membuf_secure (&data, 64);
1190 err = assuan_transact (agent_ctx, line,
1191 put_membuf_cb, &data,
1192 default_inq_cb, &inq_parm, NULL, NULL);
1195 xfree (get_membuf (&data, NULL));
1198 put_membuf (&data, "", 1);
1199 *r_passphrase = get_membuf (&data, NULL);
1201 err = gpg_error_from_syserror ();
1208 /* Retrieve a key encryption key from the agent. With FOREXPORT true
1209 the key shall be use for export, with false for import. On success
1210 the new key is stored at R_KEY and its length at R_KEKLEN. */
1212 gpgsm_agent_keywrap_key (ctrl_t ctrl, int forexport,
1213 void **r_kek, size_t *r_keklen)
1219 char line[ASSUAN_LINELENGTH];
1220 struct default_inq_parm_s inq_parm = { ctrl, agent_ctx };
1223 err = start_agent (ctrl);
1227 snprintf (line, DIM(line)-1, "KEYWRAP_KEY %s",
1228 forexport? "--export":"--import");
1230 init_membuf_secure (&data, 64);
1231 err = assuan_transact (agent_ctx, line,
1232 put_membuf_cb, &data,
1233 default_inq_cb, &inq_parm, NULL, NULL);
1236 xfree (get_membuf (&data, &len));
1239 buf = get_membuf (&data, &len);
1241 return gpg_error_from_syserror ();
1250 /* Handle the inquiry for an IMPORT_KEY command. */
1252 inq_import_key_parms (void *opaque, const char *line)
1254 struct import_key_parm_s *parm = opaque;
1257 if (has_leading_keyword (line, "KEYDATA"))
1259 assuan_begin_confidential (parm->ctx);
1260 err = assuan_send_data (parm->ctx, parm->key, parm->keylen);
1261 assuan_end_confidential (parm->ctx);
1265 struct default_inq_parm_s inq_parm = { parm->ctrl, parm->ctx };
1266 err = default_inq_cb (&inq_parm, line);
1273 /* Call the agent to import a key into the agent. */
1275 gpgsm_agent_import_key (ctrl_t ctrl, const void *key, size_t keylen)
1278 struct import_key_parm_s parm;
1280 err = start_agent (ctrl);
1285 parm.ctx = agent_ctx;
1287 parm.keylen = keylen;
1289 err = assuan_transact (agent_ctx, "IMPORT_KEY",
1290 NULL, NULL, inq_import_key_parms, &parm, NULL, NULL);
1296 /* Receive a secret key from the agent. KEYGRIP is the hexified
1297 keygrip, DESC a prompt to be displayed with the agent's passphrase
1298 question (needs to be plus+percent escaped). On success the key is
1299 stored as a canonical S-expression at R_RESULT and R_RESULTLEN. */
1301 gpgsm_agent_export_key (ctrl_t ctrl, const char *keygrip, const char *desc,
1302 unsigned char **r_result, size_t *r_resultlen)
1308 char line[ASSUAN_LINELENGTH];
1309 struct default_inq_parm_s inq_parm = { ctrl, agent_ctx };
1313 err = start_agent (ctrl);
1319 snprintf (line, DIM(line)-1, "SETKEYDESC %s", desc);
1320 err = assuan_transact (agent_ctx, line,
1321 NULL, NULL, NULL, NULL, NULL, NULL);
1326 snprintf (line, DIM(line)-1, "EXPORT_KEY %s", keygrip);
1328 init_membuf_secure (&data, 1024);
1329 err = assuan_transact (agent_ctx, line,
1330 put_membuf_cb, &data,
1331 default_inq_cb, &inq_parm, NULL, NULL);
1334 xfree (get_membuf (&data, &len));
1337 buf = get_membuf (&data, &len);
1339 return gpg_error_from_syserror ();