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 */
42 static assuan_context_t agent_ctx = NULL;
49 const unsigned char *ciphertext;
57 const unsigned char *sexp;
69 struct import_key_parm_s
79 /* Try to connect to the agent via socket or fork it off and work by
80 pipes. Handle the server's initial greeting */
82 start_agent (ctrl_t ctrl)
87 rc = 0; /* fixme: We need a context for each thread or
88 serialize the access to the agent (which is
89 suitable given that the agent is not MT. */
92 rc = start_new_gpg_agent (&agent_ctx,
93 GPG_ERR_SOURCE_DEFAULT,
96 opt.lc_ctype, opt.lc_messages,
98 opt.autostart, opt.verbose, DBG_ASSUAN,
101 if (!opt.autostart && gpg_err_code (rc) == GPG_ERR_NO_AGENT)
108 log_info (_("no gpg-agent running in this session\n"));
113 /* Tell the agent that we support Pinentry notifications. No
114 error checking so that it will work also with older
116 assuan_transact (agent_ctx, "OPTION allow-pinentry-notify",
117 NULL, NULL, NULL, NULL, NULL, NULL);
121 if (!ctrl->agent_seen)
123 ctrl->agent_seen = 1;
124 audit_log_ok (ctrl->audit, AUDIT_AGENT_READY, rc);
133 membuf_data_cb (void *opaque, const void *buffer, size_t length)
135 membuf_t *data = opaque;
138 put_membuf (data, buffer, length);
143 /* This is the default inquiry callback. It mainly handles the
144 Pinentry notifications. */
146 default_inq_cb (void *opaque, const char *line)
149 ctrl_t ctrl = opaque;
151 if (has_leading_keyword (line, "PINENTRY_LAUNCHED"))
153 err = gpgsm_proxy_pinentry_notify (ctrl, line);
155 log_error (_("failed to proxy %s inquiry to client\n"),
156 "PINENTRY_LAUNCHED");
157 /* We do not pass errors to avoid breaking other code. */
160 log_error ("ignoring gpg-agent inquiry '%s'\n", line);
168 /* Call the agent to do a sign operation using the key identified by
169 the hex string KEYGRIP. */
171 gpgsm_agent_pksign (ctrl_t ctrl, const char *keygrip, const char *desc,
172 unsigned char *digest, size_t digestlen, int digestalgo,
173 unsigned char **r_buf, size_t *r_buflen )
176 char *p, line[ASSUAN_LINELENGTH];
181 rc = start_agent (ctrl);
185 if (digestlen*2 + 50 > DIM(line))
186 return gpg_error (GPG_ERR_GENERAL);
188 rc = assuan_transact (agent_ctx, "RESET", NULL, NULL, NULL, NULL, NULL, NULL);
192 snprintf (line, DIM(line)-1, "SIGKEY %s", keygrip);
193 line[DIM(line)-1] = 0;
194 rc = assuan_transact (agent_ctx, line, NULL, NULL, NULL, NULL, NULL, NULL);
200 snprintf (line, DIM(line)-1, "SETKEYDESC %s", desc);
201 line[DIM(line)-1] = 0;
202 rc = assuan_transact (agent_ctx, line,
203 NULL, NULL, NULL, NULL, NULL, NULL);
208 sprintf (line, "SETHASH %d ", digestalgo);
209 p = line + strlen (line);
210 for (i=0; i < digestlen ; i++, p += 2 )
211 sprintf (p, "%02X", digest[i]);
212 rc = assuan_transact (agent_ctx, line, NULL, NULL, NULL, NULL, NULL, NULL);
216 init_membuf (&data, 1024);
217 rc = assuan_transact (agent_ctx, "PKSIGN",
218 membuf_data_cb, &data, default_inq_cb, ctrl,
222 xfree (get_membuf (&data, &len));
225 *r_buf = get_membuf (&data, r_buflen);
227 if (!gcry_sexp_canon_len (*r_buf, *r_buflen, NULL, NULL))
229 xfree (*r_buf); *r_buf = NULL;
230 return gpg_error (GPG_ERR_INV_VALUE);
233 return *r_buf? 0 : out_of_core ();
237 /* Call the scdaemon to do a sign operation using the key identified by
238 the hex string KEYID. */
240 gpgsm_scd_pksign (ctrl_t ctrl, const char *keyid, const char *desc,
241 unsigned char *digest, size_t digestlen, int digestalgo,
242 unsigned char **r_buf, size_t *r_buflen )
245 char *p, line[ASSUAN_LINELENGTH];
249 unsigned char *sigbuf;
258 case GCRY_MD_SHA1: hashopt = "--hash=sha1"; break;
259 case GCRY_MD_RMD160:hashopt = "--hash=rmd160"; break;
260 case GCRY_MD_MD5: hashopt = "--hash=md5"; break;
261 case GCRY_MD_SHA256:hashopt = "--hash=sha256"; break;
263 return gpg_error (GPG_ERR_DIGEST_ALGO);
266 rc = start_agent (ctrl);
270 if (digestlen*2 + 50 > DIM(line))
271 return gpg_error (GPG_ERR_GENERAL);
273 p = stpcpy (line, "SCD SETDATA " );
274 for (i=0; i < digestlen ; i++, p += 2 )
275 sprintf (p, "%02X", digest[i]);
276 rc = assuan_transact (agent_ctx, line, NULL, NULL, NULL, NULL, NULL, NULL);
280 init_membuf (&data, 1024);
282 snprintf (line, DIM(line)-1, "SCD PKSIGN %s %s", hashopt, keyid);
283 line[DIM(line)-1] = 0;
284 rc = assuan_transact (agent_ctx, line,
285 membuf_data_cb, &data, default_inq_cb, ctrl,
289 xfree (get_membuf (&data, &len));
292 sigbuf = get_membuf (&data, &sigbuflen);
294 /* Create an S-expression from it which is formatted like this:
295 "(7:sig-val(3:rsa(1:sSIGBUFLEN:SIGBUF)))" Fixme: If a card ever
296 creates non-RSA keys we need to change things. */
297 *r_buflen = 21 + 11 + sigbuflen + 4;
298 p = xtrymalloc (*r_buflen);
299 *r_buf = (unsigned char*)p;
305 p = stpcpy (p, "(7:sig-val(3:rsa(1:s" );
306 sprintf (p, "%u:", (unsigned int)sigbuflen);
308 memcpy (p, sigbuf, sigbuflen);
313 assert (gcry_sexp_canon_len (*r_buf, *r_buflen, NULL, NULL));
320 /* Handle a CIPHERTEXT inquiry. Note, we only send the data,
321 assuan_transact takes care of flushing and writing the end */
323 inq_ciphertext_cb (void *opaque, const char *line)
325 struct cipher_parm_s *parm = opaque;
328 if (has_leading_keyword (line, "CIPHERTEXT"))
330 assuan_begin_confidential (parm->ctx);
331 rc = assuan_send_data (parm->ctx, parm->ciphertext, parm->ciphertextlen);
332 assuan_end_confidential (parm->ctx);
335 rc = default_inq_cb (parm->ctrl, line);
341 /* Call the agent to do a decrypt operation using the key identified by
342 the hex string KEYGRIP. */
344 gpgsm_agent_pkdecrypt (ctrl_t ctrl, const char *keygrip, const char *desc,
345 ksba_const_sexp_t ciphertext,
346 char **r_buf, size_t *r_buflen )
349 char line[ASSUAN_LINELENGTH];
351 struct cipher_parm_s cipher_parm;
353 char *p, *buf, *endp;
354 size_t ciphertextlen;
356 if (!keygrip || strlen(keygrip) != 40 || !ciphertext || !r_buf || !r_buflen)
357 return gpg_error (GPG_ERR_INV_VALUE);
360 ciphertextlen = gcry_sexp_canon_len (ciphertext, 0, NULL, NULL);
362 return gpg_error (GPG_ERR_INV_VALUE);
364 rc = start_agent (ctrl);
368 rc = assuan_transact (agent_ctx, "RESET", NULL, NULL, NULL, NULL, NULL, NULL);
372 assert ( DIM(line) >= 50 );
373 snprintf (line, DIM(line)-1, "SETKEY %s", keygrip);
374 line[DIM(line)-1] = 0;
375 rc = assuan_transact (agent_ctx, line, NULL, NULL, NULL, NULL, NULL, NULL);
381 snprintf (line, DIM(line)-1, "SETKEYDESC %s", desc);
382 line[DIM(line)-1] = 0;
383 rc = assuan_transact (agent_ctx, line,
384 NULL, NULL, NULL, NULL, NULL, NULL);
389 init_membuf (&data, 1024);
390 cipher_parm.ctrl = ctrl;
391 cipher_parm.ctx = agent_ctx;
392 cipher_parm.ciphertext = ciphertext;
393 cipher_parm.ciphertextlen = ciphertextlen;
394 rc = assuan_transact (agent_ctx, "PKDECRYPT",
395 membuf_data_cb, &data,
396 inq_ciphertext_cb, &cipher_parm, NULL, NULL);
399 xfree (get_membuf (&data, &len));
403 put_membuf (&data, "", 1); /* Make sure it is 0 terminated. */
404 buf = get_membuf (&data, &len);
406 return gpg_error (GPG_ERR_ENOMEM);
407 assert (len); /* (we forced Nul termination.) */
411 if (len < 13 || memcmp (buf, "(5:value", 8) ) /* "(5:valueN:D)\0" */
412 return gpg_error (GPG_ERR_INV_SEXP);
413 len -= 11; /* Count only the data of the second part. */
414 p = buf + 8; /* Skip leading parenthesis and the value tag. */
418 /* For compatibility with older gpg-agents handle the old style
419 incomplete S-exps. */
420 len--; /* Do not count the Nul. */
424 n = strtoul (p, &endp, 10);
425 if (!n || *endp != ':')
426 return gpg_error (GPG_ERR_INV_SEXP);
429 return gpg_error (GPG_ERR_INV_SEXP); /* Oops: Inconsistent S-Exp. */
431 memmove (buf, endp, n);
442 /* Handle a KEYPARMS inquiry. Note, we only send the data,
443 assuan_transact takes care of flushing and writing the end */
445 inq_genkey_parms (void *opaque, const char *line)
447 struct genkey_parm_s *parm = opaque;
450 if (has_leading_keyword (line, "KEYPARAM"))
452 rc = assuan_send_data (parm->ctx, parm->sexp, parm->sexplen);
455 rc = default_inq_cb (parm->ctrl, line);
462 /* Call the agent to generate a newkey */
464 gpgsm_agent_genkey (ctrl_t ctrl,
465 ksba_const_sexp_t keyparms, ksba_sexp_t *r_pubkey)
468 struct genkey_parm_s gk_parm;
474 rc = start_agent (ctrl);
478 rc = assuan_transact (agent_ctx, "RESET", NULL, NULL, NULL, NULL, NULL, NULL);
482 init_membuf (&data, 1024);
484 gk_parm.ctx = agent_ctx;
485 gk_parm.sexp = keyparms;
486 gk_parm.sexplen = gcry_sexp_canon_len (keyparms, 0, NULL, NULL);
487 if (!gk_parm.sexplen)
488 return gpg_error (GPG_ERR_INV_VALUE);
489 rc = assuan_transact (agent_ctx, "GENKEY",
490 membuf_data_cb, &data,
491 inq_genkey_parms, &gk_parm, NULL, NULL);
494 xfree (get_membuf (&data, &len));
497 buf = get_membuf (&data, &len);
499 return gpg_error (GPG_ERR_ENOMEM);
500 if (!gcry_sexp_canon_len (buf, len, NULL, NULL))
503 return gpg_error (GPG_ERR_INV_SEXP);
510 /* Call the agent to read the public key part for a given keygrip. If
511 FROMCARD is true, the key is directly read from the current
512 smartcard. In this case HEXKEYGRIP should be the keyID
515 gpgsm_agent_readkey (ctrl_t ctrl, int fromcard, const char *hexkeygrip,
516 ksba_sexp_t *r_pubkey)
522 char line[ASSUAN_LINELENGTH];
525 rc = start_agent (ctrl);
529 rc = assuan_transact (agent_ctx, "RESET",NULL, NULL, NULL, NULL, NULL, NULL);
533 snprintf (line, DIM(line)-1, "%sREADKEY %s",
534 fromcard? "SCD ":"", hexkeygrip);
535 line[DIM(line)-1] = 0;
537 init_membuf (&data, 1024);
538 rc = assuan_transact (agent_ctx, line,
539 membuf_data_cb, &data,
540 default_inq_cb, ctrl, NULL, NULL);
543 xfree (get_membuf (&data, &len));
546 buf = get_membuf (&data, &len);
548 return gpg_error (GPG_ERR_ENOMEM);
549 if (!gcry_sexp_canon_len (buf, len, NULL, NULL))
552 return gpg_error (GPG_ERR_INV_SEXP);
560 /* Take the serial number from LINE and return it verbatim in a newly
561 allocated string. We make sure that only hex characters are
564 store_serialno (const char *line)
569 for (s=line; hexdigitp (s); s++)
571 p = xtrymalloc (s + 1 - line);
574 memcpy (p, line, s-line);
581 /* Callback for the gpgsm_agent_serialno fucntion. */
583 scd_serialno_status_cb (void *opaque, const char *line)
585 char **r_serialno = opaque;
586 const char *keyword = line;
589 for (keywordlen=0; *line && !spacep (line); line++, keywordlen++)
591 while (spacep (line))
594 if (keywordlen == 8 && !memcmp (keyword, "SERIALNO", keywordlen))
597 *r_serialno = store_serialno (line);
604 /* Call the agent to read the serial number of the current card. */
606 gpgsm_agent_scd_serialno (ctrl_t ctrl, char **r_serialno)
609 char *serialno = NULL;
612 rc = start_agent (ctrl);
616 rc = assuan_transact (agent_ctx, "SCD SERIALNO",
618 default_inq_cb, ctrl,
619 scd_serialno_status_cb, &serialno);
620 if (!rc && !serialno)
621 rc = gpg_error (GPG_ERR_INTERNAL);
627 *r_serialno = serialno;
633 /* Callback for the gpgsm_agent_serialno fucntion. */
635 scd_keypairinfo_status_cb (void *opaque, const char *line)
637 strlist_t *listaddr = opaque;
638 const char *keyword = line;
643 for (keywordlen=0; *line && !spacep (line); line++, keywordlen++)
645 while (spacep (line))
648 if (keywordlen == 11 && !memcmp (keyword, "KEYPAIRINFO", keywordlen))
650 sl = append_to_strlist (listaddr, line);
652 /* Make sure that we only have two tokes so that future
653 extensions of the format won't change the format expected by
655 while (*p && !spacep (p))
661 while (*p && !spacep (p))
671 /* Call the agent to read the keypairinfo lines of the current card.
672 The list is returned as a string made up of the keygrip, a space
675 gpgsm_agent_scd_keypairinfo (ctrl_t ctrl, strlist_t *r_list)
678 strlist_t list = NULL;
681 rc = start_agent (ctrl);
685 rc = assuan_transact (agent_ctx, "SCD LEARN --force",
687 default_inq_cb, ctrl,
688 scd_keypairinfo_status_cb, &list);
690 rc = gpg_error (GPG_ERR_NO_DATA);
703 istrusted_status_cb (void *opaque, const char *line)
705 struct rootca_flags_s *flags = opaque;
708 if ((s = has_leading_keyword (line, "TRUSTLISTFLAG")))
711 if (has_leading_keyword (line, "relax"))
713 else if (has_leading_keyword (line, "cm"))
714 flags->chain_model = 1;
721 /* Ask the agent whether the certificate is in the list of trusted
722 keys. The certificate is either specified by the CERT object or by
723 the fingerprint HEXFPR. ROOTCA_FLAGS is guaranteed to be cleared
726 gpgsm_agent_istrusted (ctrl_t ctrl, ksba_cert_t cert, const char *hexfpr,
727 struct rootca_flags_s *rootca_flags)
730 char line[ASSUAN_LINELENGTH];
732 memset (rootca_flags, 0, sizeof *rootca_flags);
735 return gpg_error (GPG_ERR_INV_ARG);
737 rc = start_agent (ctrl);
743 snprintf (line, DIM(line)-1, "ISTRUSTED %s", hexfpr);
744 line[DIM(line)-1] = 0;
750 fpr = gpgsm_get_fingerprint_hexstring (cert, GCRY_MD_SHA1);
753 log_error ("error getting the fingerprint\n");
754 return gpg_error (GPG_ERR_GENERAL);
757 snprintf (line, DIM(line)-1, "ISTRUSTED %s", fpr);
758 line[DIM(line)-1] = 0;
762 rc = assuan_transact (agent_ctx, line, NULL, NULL, NULL, NULL,
763 istrusted_status_cb, rootca_flags);
765 rootca_flags->valid = 1;
769 /* Ask the agent to mark CERT as a trusted Root-CA one */
771 gpgsm_agent_marktrusted (ctrl_t ctrl, ksba_cert_t cert)
774 char *fpr, *dn, *dnfmt;
775 char line[ASSUAN_LINELENGTH];
777 rc = start_agent (ctrl);
781 fpr = gpgsm_get_fingerprint_hexstring (cert, GCRY_MD_SHA1);
784 log_error ("error getting the fingerprint\n");
785 return gpg_error (GPG_ERR_GENERAL);
788 dn = ksba_cert_get_issuer (cert, 0);
792 return gpg_error (GPG_ERR_GENERAL);
794 dnfmt = gpgsm_format_name2 (dn, 0);
797 return gpg_error_from_syserror ();
798 snprintf (line, DIM(line)-1, "MARKTRUSTED %s S %s", fpr, dnfmt);
799 line[DIM(line)-1] = 0;
803 rc = assuan_transact (agent_ctx, line, NULL, NULL,
804 default_inq_cb, ctrl, NULL, NULL);
810 /* Ask the agent whether the a corresponding secret key is available
811 for the given keygrip */
813 gpgsm_agent_havekey (ctrl_t ctrl, const char *hexkeygrip)
816 char line[ASSUAN_LINELENGTH];
818 rc = start_agent (ctrl);
822 if (!hexkeygrip || strlen (hexkeygrip) != 40)
823 return gpg_error (GPG_ERR_INV_VALUE);
825 snprintf (line, DIM(line)-1, "HAVEKEY %s", hexkeygrip);
826 line[DIM(line)-1] = 0;
828 rc = assuan_transact (agent_ctx, line, NULL, NULL, NULL, NULL, NULL, NULL);
834 learn_status_cb (void *opaque, const char *line)
836 struct learn_parm_s *parm = opaque;
839 /* Pass progress data to the caller. */
840 if ((s = has_leading_keyword (line, "PROGRESS")))
845 if (gpgsm_status (parm->ctrl, STATUS_PROGRESS, line))
846 return gpg_error (GPG_ERR_ASS_CANCELED);
853 learn_cb (void *opaque, const void *buffer, size_t length)
855 struct learn_parm_s *parm = opaque;
866 put_membuf (parm->data, buffer, length);
869 /* END encountered - process what we have */
870 buf = get_membuf (parm->data, &len);
873 parm->error = gpg_error (GPG_ERR_ENOMEM);
877 if (gpgsm_status (parm->ctrl, STATUS_PROGRESS, "learncard C 0 0"))
878 return gpg_error (GPG_ERR_ASS_CANCELED);
880 /* FIXME: this should go into import.c */
881 rc = ksba_cert_new (&cert);
887 rc = ksba_cert_init_from_mem (cert, buf, len);
890 log_error ("failed to parse a certificate: %s\n", gpg_strerror (rc));
891 ksba_cert_release (cert);
896 /* We do not store a certifciate with missing issuers as ephemeral
897 because we can assume that the --learn-card command has been used
899 rc = gpgsm_basic_cert_check (parm->ctrl, cert);
900 if (rc && gpg_err_code (rc) != GPG_ERR_MISSING_CERT
901 && gpg_err_code (rc) != GPG_ERR_MISSING_ISSUER_CERT)
902 log_error ("invalid certificate: %s\n", gpg_strerror (rc));
907 if (!keydb_store_cert (cert, 0, &existed))
909 if (opt.verbose > 1 && existed)
910 log_info ("certificate already in DB\n");
911 else if (opt.verbose && !existed)
912 log_info ("certificate imported\n");
916 ksba_cert_release (cert);
917 init_membuf (parm->data, 4096);
921 /* Call the agent to learn about a smartcard */
923 gpgsm_agent_learn (ctrl_t ctrl)
926 struct learn_parm_s learn_parm;
930 rc = start_agent (ctrl);
934 init_membuf (&data, 4096);
935 learn_parm.error = 0;
936 learn_parm.ctrl = ctrl;
937 learn_parm.ctx = agent_ctx;
938 learn_parm.data = &data;
939 rc = assuan_transact (agent_ctx, "LEARN --send",
940 learn_cb, &learn_parm,
942 learn_status_cb, &learn_parm);
943 xfree (get_membuf (&data, &len));
946 return learn_parm.error;
950 /* Ask the agent to change the passphrase of the key identified by
951 HEXKEYGRIP. If DESC is not NULL, display instead of the default
952 description message. */
954 gpgsm_agent_passwd (ctrl_t ctrl, const char *hexkeygrip, const char *desc)
957 char line[ASSUAN_LINELENGTH];
959 rc = start_agent (ctrl);
963 if (!hexkeygrip || strlen (hexkeygrip) != 40)
964 return gpg_error (GPG_ERR_INV_VALUE);
968 snprintf (line, DIM(line)-1, "SETKEYDESC %s", desc);
969 line[DIM(line)-1] = 0;
970 rc = assuan_transact (agent_ctx, line,
971 NULL, NULL, NULL, NULL, NULL, NULL);
976 snprintf (line, DIM(line)-1, "PASSWD %s", hexkeygrip);
977 line[DIM(line)-1] = 0;
979 rc = assuan_transact (agent_ctx, line, NULL, NULL,
980 default_inq_cb, ctrl, NULL, NULL);
986 /* Ask the agent to pop up a confirmation dialog with the text DESC
987 and an okay and cancel button. */
989 gpgsm_agent_get_confirmation (ctrl_t ctrl, const char *desc)
992 char line[ASSUAN_LINELENGTH];
994 rc = start_agent (ctrl);
998 snprintf (line, DIM(line)-1, "GET_CONFIRMATION %s", desc);
999 line[DIM(line)-1] = 0;
1001 rc = assuan_transact (agent_ctx, line, NULL, NULL,
1002 default_inq_cb, ctrl, NULL, NULL);
1008 /* Return 0 if the agent is alive. This is useful to make sure that
1009 an agent has been started. */
1011 gpgsm_agent_send_nop (ctrl_t ctrl)
1015 rc = start_agent (ctrl);
1017 rc = assuan_transact (agent_ctx, "NOP",
1018 NULL, NULL, NULL, NULL, NULL, NULL);
1025 keyinfo_status_cb (void *opaque, const char *line)
1027 char **serialno = opaque;
1030 if ((s = has_leading_keyword (line, "KEYINFO")) && !*serialno)
1032 s = strchr (s, ' ');
1033 if (s && s[1] == 'T' && s[2] == ' ' && s[3])
1036 s2 = strchr (s, ' ');
1039 *serialno = xtrymalloc ((s2 - s)+1);
1042 memcpy (*serialno, s, s2 - s);
1043 (*serialno)[s2 - s] = 0;
1051 /* Return the serial number for a secret key. If the returned serial
1052 number is NULL, the key is not stored on a smartcard. Caller needs
1053 to free R_SERIALNO. */
1055 gpgsm_agent_keyinfo (ctrl_t ctrl, const char *hexkeygrip, char **r_serialno)
1058 char line[ASSUAN_LINELENGTH];
1059 char *serialno = NULL;
1063 err = start_agent (ctrl);
1067 if (!hexkeygrip || strlen (hexkeygrip) != 40)
1068 return gpg_error (GPG_ERR_INV_VALUE);
1070 snprintf (line, DIM(line)-1, "KEYINFO %s", hexkeygrip);
1071 line[DIM(line)-1] = 0;
1073 err = assuan_transact (agent_ctx, line, NULL, NULL, NULL, NULL,
1074 keyinfo_status_cb, &serialno);
1075 if (!err && serialno)
1077 /* Sanity check for bad characters. */
1078 if (strpbrk (serialno, ":\n\r"))
1079 err = GPG_ERR_INV_VALUE;
1084 *r_serialno = serialno;
1090 /* Ask for the passphrase (this is used for pkcs#12 import/export. On
1091 success the caller needs to free the string stored at R_PASSPHRASE.
1092 On error NULL will be stored at R_PASSPHRASE and an appropriate
1093 error code returned. If REPEAT is true the agent tries to get a
1094 new passphrase (i.e. asks the user to confirm it). */
1096 gpgsm_agent_ask_passphrase (ctrl_t ctrl, const char *desc_msg, int repeat,
1097 char **r_passphrase)
1100 char line[ASSUAN_LINELENGTH];
1104 *r_passphrase = NULL;
1106 err = start_agent (ctrl);
1110 if (desc_msg && *desc_msg && !(arg4 = percent_plus_escape (desc_msg)))
1111 return gpg_error_from_syserror ();
1113 snprintf (line, DIM(line)-1, "GET_PASSPHRASE --data%s -- X X X %s",
1114 repeat? " --repeat=1 --check --qualitybar":"",
1118 init_membuf_secure (&data, 64);
1119 err = assuan_transact (agent_ctx, line,
1120 membuf_data_cb, &data,
1121 default_inq_cb, NULL, NULL, NULL);
1124 xfree (get_membuf (&data, NULL));
1127 put_membuf (&data, "", 1);
1128 *r_passphrase = get_membuf (&data, NULL);
1130 err = gpg_error_from_syserror ();
1137 /* Retrieve a key encryption key from the agent. With FOREXPORT true
1138 the key shall be use for export, with false for import. On success
1139 the new key is stored at R_KEY and its length at R_KEKLEN. */
1141 gpgsm_agent_keywrap_key (ctrl_t ctrl, int forexport,
1142 void **r_kek, size_t *r_keklen)
1148 char line[ASSUAN_LINELENGTH];
1151 err = start_agent (ctrl);
1155 snprintf (line, DIM(line)-1, "KEYWRAP_KEY %s",
1156 forexport? "--export":"--import");
1158 init_membuf_secure (&data, 64);
1159 err = assuan_transact (agent_ctx, line,
1160 membuf_data_cb, &data,
1161 default_inq_cb, ctrl, NULL, NULL);
1164 xfree (get_membuf (&data, &len));
1167 buf = get_membuf (&data, &len);
1169 return gpg_error_from_syserror ();
1178 /* Handle the inquiry for an IMPORT_KEY command. */
1180 inq_import_key_parms (void *opaque, const char *line)
1182 struct import_key_parm_s *parm = opaque;
1185 if (has_leading_keyword (line, "KEYDATA"))
1187 assuan_begin_confidential (parm->ctx);
1188 err = assuan_send_data (parm->ctx, parm->key, parm->keylen);
1189 assuan_end_confidential (parm->ctx);
1192 err = default_inq_cb (parm->ctrl, line);
1198 /* Call the agent to import a key into the agent. */
1200 gpgsm_agent_import_key (ctrl_t ctrl, const void *key, size_t keylen)
1203 struct import_key_parm_s parm;
1205 err = start_agent (ctrl);
1210 parm.ctx = agent_ctx;
1212 parm.keylen = keylen;
1214 err = assuan_transact (agent_ctx, "IMPORT_KEY",
1215 NULL, NULL, inq_import_key_parms, &parm, NULL, NULL);
1221 /* Receive a secret key from the agent. KEYGRIP is the hexified
1222 keygrip, DESC a prompt to be displayed with the agent's passphrase
1223 question (needs to be plus+percent escaped). On success the key is
1224 stored as a canonical S-expression at R_RESULT and R_RESULTLEN. */
1226 gpgsm_agent_export_key (ctrl_t ctrl, const char *keygrip, const char *desc,
1227 unsigned char **r_result, size_t *r_resultlen)
1233 char line[ASSUAN_LINELENGTH];
1237 err = start_agent (ctrl);
1243 snprintf (line, DIM(line)-1, "SETKEYDESC %s", desc);
1244 err = assuan_transact (agent_ctx, line,
1245 NULL, NULL, NULL, NULL, NULL, NULL);
1250 snprintf (line, DIM(line)-1, "EXPORT_KEY %s", keygrip);
1252 init_membuf_secure (&data, 1024);
1253 err = assuan_transact (agent_ctx, line,
1254 membuf_data_cb, &data,
1255 default_inq_cb, ctrl, NULL, NULL);
1258 xfree (get_membuf (&data, &len));
1261 buf = get_membuf (&data, &len);
1263 return gpg_error_from_syserror ();