1 /* command.c - gpg-agent command handler
2 * Copyright (C) 2001, 2002, 2003, 2004, 2005,
3 * 2006, 2008, 2009 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/>.
21 /* FIXME: we should not use the default assuan buffering but setup
22 some buffering in secure mempory to protect session keys etc. */
33 #include <sys/types.h>
42 /* maximum allowed size of the inquired ciphertext */
43 #define MAXLEN_CIPHERTEXT 4096
44 /* maximum allowed size of the key parameters */
45 #define MAXLEN_KEYPARAM 1024
47 #define set_error(e,t) assuan_set_error (ctx, gpg_error (e), (t))
50 #if MAX_DIGEST_LEN < 20
51 #error MAX_DIGEST_LEN shorter than keygrip
54 /* Data used to associate an Assuan context with local server data */
57 assuan_context_t assuan_ctx
;
59 int use_cache_for_signing
;
60 char *keydesc
; /* Allocated description for the next key
62 int pause_io_logging
; /* Used to suppress I/O logging during a command */
63 #ifdef HAVE_W32_SYSTEM
64 int stopme
; /* If set to true the agent will be terminated after
65 the end of this session. */
67 int allow_pinentry_notify
; /* Set if pinentry notifications should
72 /* An entry for the getval/putval commands. */
75 struct putval_item_s
*next
;
76 size_t off
; /* Offset to the value into DATA. */
77 size_t len
; /* Length of the value. */
78 char d
[1]; /* Key | Nul | value. */
82 /* A list of key value pairs fpr the getval/putval commands. */
83 static struct putval_item_s
*putval_list
;
87 /* To help polling clients, we keep track of the number of certain
88 events. This structure keeps those counters. The counters are
89 integers and there should be no problem if they are overflowing as
90 callers need to check only whether a counter changed. The actual
91 values are not meaningful. */
94 /* Incremented if any of the other counters below changed. */
97 /* Incremented if a key is added or removed from the internal privat
101 /* Incremented if a change of the card readers stati has been
109 /* Local prototypes. */
110 static int command_has_option (const char *cmd
, const char *cmdopt
);
115 /* Release the memory buffer MB but first wipe out the used memory. */
117 clear_outbuf (membuf_t
*mb
)
122 p
= get_membuf (mb
, &n
);
131 /* Write the content of memory buffer MB as assuan data to CTX and
132 wipe the buffer out afterwards. */
134 write_and_clear_outbuf (assuan_context_t ctx
, membuf_t
*mb
)
140 p
= get_membuf (mb
, &n
);
142 return out_of_core ();
143 ae
= assuan_send_data (ctx
, p
, n
);
151 reset_notify (assuan_context_t ctx
)
153 ctrl_t ctrl
= assuan_get_pointer (ctx
);
155 memset (ctrl
->keygrip
, 0, 20);
156 ctrl
->have_keygrip
= 0;
157 ctrl
->digest
.valuelen
= 0;
159 xfree (ctrl
->server_local
->keydesc
);
160 ctrl
->server_local
->keydesc
= NULL
;
164 /* Check whether the option NAME appears in LINE */
166 has_option (const char *line
, const char *name
)
169 int n
= strlen (name
);
171 s
= strstr (line
, name
);
172 return (s
&& (s
== line
|| spacep (s
-1)) && (!s
[n
] || spacep (s
+n
)));
175 /* Same as has_option but does only test for the name of the option
176 and ignores an argument, i.e. with NAME being "--hash" it would
177 return true for "--hash" as well as for "--hash=foo". */
179 has_option_name (const char *line
, const char *name
)
182 int n
= strlen (name
);
184 s
= strstr (line
, name
);
185 return (s
&& (s
== line
|| spacep (s
-1))
186 && (!s
[n
] || spacep (s
+n
) || s
[n
] == '='));
189 /* Return a pointer to the argument of the option with NAME. If such
190 an option is not given, it returns NULL. */
192 option_value (const char *line
, const char *name
)
195 int n
= strlen (name
);
197 s
= strstr (line
, name
);
198 if (s
&& (s
== line
|| spacep (s
-1))
199 && s
[n
] && (spacep (s
+n
) || s
[n
] == '='))
202 s
+= strspn (s
, " ");
203 if (*s
&& !spacep(s
))
210 /* Skip over options. It is assumed that leading spaces have been
211 removed (this is the case for lines passed to a handler from
212 assuan). Blanks after the options are also removed. */
214 skip_options (char *line
)
216 while ( *line
== '-' && line
[1] == '-' )
218 while (*line
&& !spacep (line
))
220 while (spacep (line
))
227 /* Replace all '+' by a blank. */
229 plus_to_blank (char *s
)
239 /* Parse a hex string. Return an Assuan error code or 0 on success and the
240 length of the parsed string in LEN. */
242 parse_hexstring (assuan_context_t ctx
, const char *string
, size_t *len
)
247 /* parse the hash value */
248 for (p
=string
, n
=0; hexdigitp (p
); p
++, n
++)
250 if (*p
!= ' ' && *p
!= '\t' && *p
)
251 return set_error (GPG_ERR_ASS_PARAMETER
, "invalid hexstring");
253 return set_error (GPG_ERR_ASS_PARAMETER
, "odd number of digits");
258 /* Parse the keygrip in STRING into the provided buffer BUF. BUF must
259 provide space for 20 bytes. BUF is not changed if the function
262 parse_keygrip (assuan_context_t ctx
, const char *string
, unsigned char *buf
)
267 rc
= parse_hexstring (ctx
, string
, &n
);
272 return set_error (GPG_ERR_ASS_PARAMETER
, "invalid length of keygrip");
274 if (hex2bin (string
, buf
, 20) < 0)
275 return set_error (GPG_ERR_BUG
, "hex2bin");
281 /* Write an assuan status line. */
283 agent_write_status (ctrl_t ctrl
, const char *keyword
, ...)
288 assuan_context_t ctx
= ctrl
->server_local
->assuan_ctx
;
292 va_start (arg_ptr
, keyword
);
296 while ( (text
= va_arg (arg_ptr
, const char *)) )
303 for ( ; *text
&& n
< DIM (buf
)-3; n
++, text
++)
310 else if (*text
== '\r')
320 err
= assuan_write_status (ctx
, keyword
, buf
);
327 /* Helper to notify the client about a launched Pinentry. Because
328 that might disturb some older clients, this is only done if enabled
329 via an option. Returns an gpg error code. */
331 agent_inq_pinentry_launched (ctrl_t ctrl
, unsigned long pid
)
335 if (!ctrl
|| !ctrl
->server_local
336 || !ctrl
->server_local
->allow_pinentry_notify
)
338 snprintf (line
, DIM(line
)-1, "PINENTRY_LAUNCHED %lu", pid
);
339 return assuan_inquire (ctrl
->server_local
->assuan_ctx
, line
, NULL
, NULL
, 0);
346 Return a a status line named EVENTCOUNTER with the current values
347 of all event counters. The values are decimal numbers in the range
348 0 to UINT_MAX and wrapping around to 0. The actual values should
349 not be relied upon, they shall only be used to detect a change.
351 The currently defined counters are:
353 ANY - Incremented with any change of any of the other counters.
354 KEY - Incremented for added or removed private keys.
355 CARD - Incremented for changes of the card readers stati.
358 cmd_geteventcounter (assuan_context_t ctx
, char *line
)
360 ctrl_t ctrl
= assuan_get_pointer (ctx
);
361 char any_counter
[25];
362 char key_counter
[25];
363 char card_counter
[25];
367 snprintf (any_counter
, sizeof any_counter
, "%u", eventcounter
.any
);
368 snprintf (key_counter
, sizeof key_counter
, "%u", eventcounter
.key
);
369 snprintf (card_counter
, sizeof card_counter
, "%u", eventcounter
.card
);
371 return agent_write_status (ctrl
, "EVENTCOUNTER",
379 /* This function should be called once for all key removals or
380 additions. This function is assured not to do any context
383 bump_key_eventcounter (void)
389 /* This function should be called for all card reader status
390 changes. This function is assured not to do any context
393 bump_card_eventcounter (void)
402 /* ISTRUSTED <hexstring_with_fingerprint>
404 Return OK when we have an entry with this fingerprint in our
407 cmd_istrusted (assuan_context_t ctx
, char *line
)
409 ctrl_t ctrl
= assuan_get_pointer (ctx
);
414 /* Parse the fingerprint value. */
415 for (p
=line
,n
=0; hexdigitp (p
); p
++, n
++)
417 if (*p
|| !(n
== 40 || n
== 32))
418 return set_error (GPG_ERR_ASS_PARAMETER
, "invalid fingerprint");
422 strcpy (fpr
, "00000000");
425 for (p
=line
; i
< 40; p
++, i
++)
426 fpr
[i
] = *p
>= 'a'? (*p
& 0xdf): *p
;
428 rc
= agent_istrusted (ctrl
, fpr
, NULL
);
429 if (!rc
|| gpg_err_code (rc
) == GPG_ERR_NOT_TRUSTED
)
431 else if (rc
== -1 || gpg_err_code (rc
) == GPG_ERR_EOF
)
432 return gpg_error (GPG_ERR_NOT_TRUSTED
);
435 log_error ("command is_trusted failed: %s\n", gpg_strerror (rc
));
442 List all entries from the trustlist */
444 cmd_listtrusted (assuan_context_t ctx
, char *line
)
450 rc
= agent_listtrusted (ctx
);
452 log_error ("command listtrusted failed: %s\n", gpg_strerror (rc
));
457 /* MARKTRUSTED <hexstring_with_fingerprint> <flag> <display_name>
459 Store a new key in into the trustlist*/
461 cmd_marktrusted (assuan_context_t ctx
, char *line
)
463 ctrl_t ctrl
= assuan_get_pointer (ctx
);
469 /* parse the fingerprint value */
470 for (p
=line
,n
=0; hexdigitp (p
); p
++, n
++)
472 if (!spacep (p
) || !(n
== 40 || n
== 32))
473 return set_error (GPG_ERR_ASS_PARAMETER
, "invalid fingerprint");
477 strcpy (fpr
, "00000000");
480 for (p
=line
; i
< 40; p
++, i
++)
481 fpr
[i
] = *p
>= 'a'? (*p
& 0xdf): *p
;
487 if ( (flag
!= 'S' && flag
!= 'P') || !spacep (p
) )
488 return set_error (GPG_ERR_ASS_PARAMETER
, "invalid flag - must be P or S");
492 rc
= agent_marktrusted (ctrl
, p
, fpr
, flag
);
494 log_error ("command marktrusted failed: %s\n", gpg_strerror (rc
));
501 /* HAVEKEY <hexstring_with_keygrip>
503 Return success when the secret key is available */
505 cmd_havekey (assuan_context_t ctx
, char *line
)
508 unsigned char buf
[20];
510 rc
= parse_keygrip (ctx
, line
, buf
);
514 if (agent_key_available (buf
))
515 return gpg_error (GPG_ERR_NO_SECKEY
);
521 /* SIGKEY <hexstring_with_keygrip>
522 SETKEY <hexstring_with_keygrip>
524 Set the key used for a sign or decrypt operation */
526 cmd_sigkey (assuan_context_t ctx
, char *line
)
529 ctrl_t ctrl
= assuan_get_pointer (ctx
);
531 rc
= parse_keygrip (ctx
, line
, ctrl
->keygrip
);
534 ctrl
->have_keygrip
= 1;
539 /* SETKEYDESC plus_percent_escaped_string
541 Set a description to be used for the next PKSIGN or PKDECRYPT
542 operation if this operation requires the entry of a passphrase. If
543 this command is not used a default text will be used. Note, that
544 this description implictly selects the label used for the entry
545 box; if the string contains the string PIN (which in general will
546 not be translated), "PIN" is used, otherwise the translation of
547 "passphrase" is used. The description string should not contain
548 blanks unless they are percent or '+' escaped.
550 The description is only valid for the next PKSIGN or PKDECRYPT
554 cmd_setkeydesc (assuan_context_t ctx
, char *line
)
556 ctrl_t ctrl
= assuan_get_pointer (ctx
);
559 for (p
=line
; *p
== ' '; p
++)
562 p
= strchr (desc
, ' ');
564 *p
= 0; /* We ignore any garbage; we might late use it for other args. */
567 return set_error (GPG_ERR_ASS_PARAMETER
, "no description given");
569 /* Note, that we only need to replace the + characters and should
570 leave the other escaping in place because the escaped string is
571 send verbatim to the pinentry which does the unescaping (but not
573 plus_to_blank (desc
);
575 xfree (ctrl
->server_local
->keydesc
);
576 ctrl
->server_local
->keydesc
= xtrystrdup (desc
);
577 if (!ctrl
->server_local
->keydesc
)
578 return out_of_core ();
583 /* SETHASH --hash=<name>|<algonumber> <hexstring>
585 The client can use this command to tell the server about the data
586 (which usually is a hash) to be signed. */
588 cmd_sethash (assuan_context_t ctx
, char *line
)
593 ctrl_t ctrl
= assuan_get_pointer (ctx
);
598 /* Parse the alternative hash options which may be used instead of
600 if (has_option_name (line
, "--hash"))
602 if (has_option (line
, "--hash=sha1"))
604 else if (has_option (line
, "--hash=sha224"))
605 algo
= GCRY_MD_SHA224
;
606 else if (has_option (line
, "--hash=sha256"))
607 algo
= GCRY_MD_SHA256
;
608 else if (has_option (line
, "--hash=sha384"))
609 algo
= GCRY_MD_SHA384
;
610 else if (has_option (line
, "--hash=sha512"))
611 algo
= GCRY_MD_SHA512
;
612 else if (has_option (line
, "--hash=rmd160"))
613 algo
= GCRY_MD_RMD160
;
614 else if (has_option (line
, "--hash=md5"))
616 else if (has_option (line
, "--hash=tls-md5sha1"))
617 algo
= MD_USER_TLS_MD5SHA1
;
619 return set_error (GPG_ERR_ASS_PARAMETER
, "invalid hash algorithm");
624 line
= skip_options (line
);
628 /* No hash option has been given: require an algo number instead */
629 algo
= (int)strtoul (line
, &endp
, 10);
630 for (line
= endp
; *line
== ' ' || *line
== '\t'; line
++)
632 if (!algo
|| gcry_md_test_algo (algo
))
633 return set_error (GPG_ERR_UNSUPPORTED_ALGORITHM
, NULL
);
635 ctrl
->digest
.algo
= algo
;
637 /* Parse the hash value. */
638 rc
= parse_hexstring (ctx
, line
, &n
);
642 if (algo
== MD_USER_TLS_MD5SHA1
&& n
== 36)
644 else if (n
!= 16 && n
!= 20 && n
!= 24
645 && n
!= 28 && n
!= 32 && n
!= 48 && n
!= 64)
646 return set_error (GPG_ERR_ASS_PARAMETER
, "unsupported length of hash");
648 if (n
> MAX_DIGEST_LEN
)
649 return set_error (GPG_ERR_ASS_PARAMETER
, "hash value to long");
651 buf
= ctrl
->digest
.value
;
652 ctrl
->digest
.valuelen
= n
;
653 for (p
=line
, n
=0; n
< ctrl
->digest
.valuelen
; p
+= 2, n
++)
655 for (; n
< ctrl
->digest
.valuelen
; n
++)
663 Perform the actual sign operation. Neither input nor output are
664 sensitive to eavesdropping. */
666 cmd_pksign (assuan_context_t ctx
, char *line
)
669 cache_mode_t cache_mode
= CACHE_MODE_NORMAL
;
670 ctrl_t ctrl
= assuan_get_pointer (ctx
);
675 if (opt
.ignore_cache_for_signing
)
676 cache_mode
= CACHE_MODE_IGNORE
;
677 else if (!ctrl
->server_local
->use_cache_for_signing
)
678 cache_mode
= CACHE_MODE_IGNORE
;
680 init_membuf (&outbuf
, 512);
682 rc
= agent_pksign (ctrl
, ctrl
->server_local
->keydesc
,
683 &outbuf
, cache_mode
);
685 clear_outbuf (&outbuf
);
687 rc
= write_and_clear_outbuf (ctx
, &outbuf
);
689 log_error ("command pksign failed: %s\n", gpg_strerror (rc
));
690 xfree (ctrl
->server_local
->keydesc
);
691 ctrl
->server_local
->keydesc
= NULL
;
695 /* PKDECRYPT <options>
697 Perform the actual decrypt operation. Input is not
698 sensitive to eavesdropping */
700 cmd_pkdecrypt (assuan_context_t ctx
, char *line
)
703 ctrl_t ctrl
= assuan_get_pointer (ctx
);
704 unsigned char *value
;
710 /* First inquire the data to decrypt */
711 rc
= assuan_inquire (ctx
, "CIPHERTEXT",
712 &value
, &valuelen
, MAXLEN_CIPHERTEXT
);
716 init_membuf (&outbuf
, 512);
718 rc
= agent_pkdecrypt (ctrl
, ctrl
->server_local
->keydesc
,
719 value
, valuelen
, &outbuf
);
722 clear_outbuf (&outbuf
);
724 rc
= write_and_clear_outbuf (ctx
, &outbuf
);
726 log_error ("command pkdecrypt failed: %s\n", gpg_strerror (rc
));
727 xfree (ctrl
->server_local
->keydesc
);
728 ctrl
->server_local
->keydesc
= NULL
;
735 Generate a new key, store the secret part and return the public
736 part. Here is an example transaction:
740 C: D (genkey (rsa (nbits 1024)))
743 S: D (rsa (n 326487324683264) (e 10001)))
748 cmd_genkey (assuan_context_t ctx
, char *line
)
750 ctrl_t ctrl
= assuan_get_pointer (ctx
);
752 unsigned char *value
;
758 /* First inquire the parameters */
759 rc
= assuan_inquire (ctx
, "KEYPARAM", &value
, &valuelen
, MAXLEN_KEYPARAM
);
763 init_membuf (&outbuf
, 512);
765 rc
= agent_genkey (ctrl
, (char*)value
, valuelen
, &outbuf
);
768 clear_outbuf (&outbuf
);
770 rc
= write_and_clear_outbuf (ctx
, &outbuf
);
772 log_error ("command genkey failed: %s\n", gpg_strerror (rc
));
779 /* READKEY <hexstring_with_keygrip>
781 Return the public key for the given keygrip. */
783 cmd_readkey (assuan_context_t ctx
, char *line
)
785 ctrl_t ctrl
= assuan_get_pointer (ctx
);
787 unsigned char grip
[20];
788 gcry_sexp_t s_pkey
= NULL
;
790 rc
= parse_keygrip (ctx
, line
, grip
);
792 return rc
; /* Return immediately as this is already an Assuan error code.*/
794 rc
= agent_public_key_from_file (ctrl
, grip
, &s_pkey
);
800 len
= gcry_sexp_sprint (s_pkey
, GCRYSEXP_FMT_CANON
, NULL
, 0);
802 buf
= xtrymalloc (len
);
804 rc
= gpg_error_from_syserror ();
807 len
= gcry_sexp_sprint (s_pkey
, GCRYSEXP_FMT_CANON
, buf
, len
);
809 rc
= assuan_send_data (ctx
, buf
, len
);
812 gcry_sexp_release (s_pkey
);
816 log_error ("command readkey failed: %s\n", gpg_strerror (rc
));
822 /* KEYINFO [--list] <keygrip>
824 Return information about the key specified by the KEYGRIP. If the
825 key is not available GPG_ERR_NOT_FOUND is returned. If the option
826 --list is given the keygrip is ignored and information about all
827 available keys are returned. The information is returned as a
828 status line with this format:
830 KEYINFO <keygrip> <type> <serialno> <idstr>
832 KEYGRIP is the keygrip.
834 TYPE is describes the type of the key:
835 'D' - Regular key stored on disk,
836 'T' - Key is stored on a smartcard (token).
839 SERIALNO is an ASCII string with the serial number of the
840 smartcard. If the serial number is not known a single
841 dash '-' is used instead.
843 IDSTR is the IDSTR used to distinguish keys on a smartcard. If it
844 is not known a dash is used instead.
846 More information may be added in the future.
849 do_one_keyinfo (ctrl_t ctrl
, const unsigned char *grip
)
854 unsigned char *shadow_info
= NULL
;
855 char *serialno
= NULL
;
857 const char *keytypestr
;
859 err
= agent_key_info_from_file (ctrl
, grip
, &keytype
, &shadow_info
);
863 /* Reformat the grip so that we use uppercase as good style. */
864 bin2hex (grip
, 20, hexgrip
);
866 if (keytype
== PRIVATE_KEY_CLEAR
867 || keytype
== PRIVATE_KEY_PROTECTED
)
869 else if (keytype
== PRIVATE_KEY_SHADOWED
)
876 err
= parse_shadow_info (shadow_info
, &serialno
, &idstr
);
881 err
= agent_write_status (ctrl
, "KEYINFO",
884 serialno
? serialno
: "-",
896 cmd_keyinfo (assuan_context_t ctx
, char *line
)
898 ctrl_t ctrl
= assuan_get_pointer (ctx
);
900 unsigned char grip
[20];
904 list_mode
= has_option (line
, "--list");
905 line
= skip_options (line
);
910 struct dirent
*dir_entry
;
913 dirname
= make_filename_try (opt
.homedir
, GNUPG_PRIVATE_KEYS_DIR
, NULL
);
916 err
= gpg_error_from_syserror ();
919 dir
= opendir (dirname
);
922 err
= gpg_error_from_syserror ();
928 while ( (dir_entry
= readdir (dir
)) )
930 if (strlen (dir_entry
->d_name
) != 44
931 || strcmp (dir_entry
->d_name
+ 40, ".key"))
933 strncpy (hexgrip
, dir_entry
->d_name
, 40);
936 if ( hex2bin (hexgrip
, grip
, 20) < 0 )
937 continue; /* Bad hex string. */
939 err
= do_one_keyinfo (ctrl
, grip
);
947 err
= parse_keygrip (ctx
, line
, grip
);
950 err
= do_one_keyinfo (ctrl
, grip
);
956 if (err
&& gpg_err_code (err
) != GPG_ERR_NOT_FOUND
)
957 log_error ("command keyinfo failed: %s\n", gpg_strerror (err
));
964 send_back_passphrase (assuan_context_t ctx
, int via_data
, const char *pw
)
969 assuan_begin_confidential (ctx
);
972 rc
= assuan_send_data (ctx
, pw
, n
);
975 char *p
= xtrymalloc_secure (n
*2+1);
977 rc
= gpg_error_from_syserror ();
981 rc
= assuan_set_okay_line (ctx
, p
);
989 /* GET_PASSPHRASE [--data] [--check] [--no-ask] [--repeat[=N]] <cache_id>
990 [<error_message> <prompt> <description>]
992 This function is usually used to ask for a passphrase to be used
993 for conventional encryption, but may also be used by programs which
994 need specal handling of passphrases. This command uses a syntax
995 which helps clients to use the agent with minimum effort. The
996 agent either returns with an error or with a OK followed by the hex
997 encoded passphrase. Note that the length of the strings is
998 implicitly limited by the maximum length of a command.
1000 If the option "--data" is used the passphrase is returned by usual
1001 data lines and not on the okay line.
1003 If the option "--check" is used the passphrase constraints checks as
1004 implemented by gpg-agent are applied. A check is not done if the
1005 passphrase has been found in the cache.
1007 If the option "--no-ask" is used and the passphrase is not in the
1008 cache the user will not be asked to enter a passphrase but the error
1009 code GPG_ERR_NO_DATA is returned.
1013 cmd_get_passphrase (assuan_context_t ctx
, char *line
)
1015 ctrl_t ctrl
= assuan_get_pointer (ctx
);
1019 char *cacheid
= NULL
, *desc
= NULL
, *prompt
= NULL
, *errtext
= NULL
;
1020 const char *desc2
= _("Please re-enter this passphrase");
1023 int opt_data
, opt_check
, opt_no_ask
, opt_repeat
= 0;
1024 char *repeat_errtext
= NULL
;
1026 opt_data
= has_option (line
, "--data");
1027 opt_check
= has_option (line
, "--check");
1028 opt_no_ask
= has_option (line
, "--no-ask");
1029 if (has_option_name (line
, "--repeat"))
1031 p
= option_value (line
, "--repeat");
1033 opt_repeat
= atoi (p
);
1037 line
= skip_options (line
);
1040 p
= strchr (cacheid
, ' ');
1047 p
= strchr (errtext
, ' ');
1054 p
= strchr (prompt
, ' ');
1061 p
= strchr (desc
, ' ');
1063 *p
= 0; /* Ignore trailing garbage. */
1067 if (!cacheid
|| !*cacheid
|| strlen (cacheid
) > 50)
1068 return set_error (GPG_ERR_ASS_PARAMETER
, "invalid length of cacheID");
1070 return set_error (GPG_ERR_ASS_PARAMETER
, "no description given");
1072 if (!strcmp (cacheid
, "X"))
1074 if (!strcmp (errtext
, "X"))
1076 if (!strcmp (prompt
, "X"))
1078 if (!strcmp (desc
, "X"))
1081 pw
= cacheid
? agent_get_cache (cacheid
, CACHE_MODE_NORMAL
, &cache_marker
)
1085 rc
= send_back_passphrase (ctx
, opt_data
, pw
);
1086 agent_unlock_cache_entry (&cache_marker
);
1088 else if (opt_no_ask
)
1089 rc
= gpg_error (GPG_ERR_NO_DATA
);
1092 /* Note, that we only need to replace the + characters and
1093 should leave the other escaping in place because the escaped
1094 string is send verbatim to the pinentry which does the
1095 unescaping (but not the + replacing) */
1097 plus_to_blank (errtext
);
1099 plus_to_blank (prompt
);
1101 plus_to_blank (desc
);
1104 rc
= agent_get_passphrase (ctrl
, &response
, desc
, prompt
,
1105 repeat_errtext
? repeat_errtext
:errtext
);
1106 xfree (repeat_errtext
);
1107 repeat_errtext
= NULL
;
1112 if (opt_check
&& check_passphrase_constraints (ctrl
, response
, 0))
1117 for (i
= 0; i
< opt_repeat
; i
++)
1121 rc
= agent_get_passphrase (ctrl
, &response2
, desc2
, prompt
,
1125 if (strcmp (response2
, response
))
1129 repeat_errtext
= try_percent_escape
1130 (_("does not match - try again"), NULL
);
1131 if (!repeat_errtext
)
1133 rc
= gpg_error_from_syserror ();
1143 agent_put_cache (cacheid
, CACHE_MODE_USER
, response
, 0);
1144 rc
= send_back_passphrase (ctx
, opt_data
, response
);
1151 log_error ("command get_passphrase failed: %s\n", gpg_strerror (rc
));
1156 /* CLEAR_PASSPHRASE <cache_id>
1158 may be used to invalidate the cache entry for a passphrase. The
1159 function returns with OK even when there is no cached passphrase.
1163 cmd_clear_passphrase (assuan_context_t ctx
, char *line
)
1165 char *cacheid
= NULL
;
1168 /* parse the stuff */
1169 for (p
=line
; *p
== ' '; p
++)
1172 p
= strchr (cacheid
, ' ');
1174 *p
= 0; /* ignore garbage */
1175 if (!cacheid
|| !*cacheid
|| strlen (cacheid
) > 50)
1176 return set_error (GPG_ERR_ASS_PARAMETER
, "invalid length of cacheID");
1178 agent_put_cache (cacheid
, CACHE_MODE_USER
, NULL
, 0);
1183 /* GET_CONFIRMATION <description>
1185 This command may be used to ask for a simple confirmation.
1186 DESCRIPTION is displayed along with a Okay and Cancel button. This
1187 command uses a syntax which helps clients to use the agent with
1188 minimum effort. The agent either returns with an error or with a
1189 OK. Note, that the length of DESCRIPTION is implicitly limited by
1190 the maximum length of a command. DESCRIPTION should not contain
1191 any spaces, those must be encoded either percent escaped or simply
1196 cmd_get_confirmation (assuan_context_t ctx
, char *line
)
1198 ctrl_t ctrl
= assuan_get_pointer (ctx
);
1203 /* parse the stuff */
1204 for (p
=line
; *p
== ' '; p
++)
1207 p
= strchr (desc
, ' ');
1209 *p
= 0; /* We ignore any garbage -may be later used for other args. */
1211 if (!desc
|| !*desc
)
1212 return set_error (GPG_ERR_ASS_PARAMETER
, "no description given");
1214 if (!strcmp (desc
, "X"))
1217 /* Note, that we only need to replace the + characters and should
1218 leave the other escaping in place because the escaped string is
1219 send verbatim to the pinentry which does the unescaping (but not
1222 plus_to_blank (desc
);
1224 rc
= agent_get_confirmation (ctrl
, desc
, NULL
, NULL
);
1226 log_error ("command get_confirmation failed: %s\n", gpg_strerror (rc
));
1234 Learn something about the currently inserted smartcard. With
1235 --send the new certificates are send back. */
1237 cmd_learn (assuan_context_t ctx
, char *line
)
1239 ctrl_t ctrl
= assuan_get_pointer (ctx
);
1242 rc
= agent_handle_learn (ctrl
, has_option (line
, "--send")? ctx
: NULL
);
1244 log_error ("command learn failed: %s\n", gpg_strerror (rc
));
1250 /* PASSWD <hexstring_with_keygrip>
1252 Change the passphrase/PIN for the key identified by keygrip in LINE. */
1254 cmd_passwd (assuan_context_t ctx
, char *line
)
1256 ctrl_t ctrl
= assuan_get_pointer (ctx
);
1258 unsigned char grip
[20];
1259 gcry_sexp_t s_skey
= NULL
;
1260 unsigned char *shadow_info
= NULL
;
1262 rc
= parse_keygrip (ctx
, line
, grip
);
1267 rc
= agent_key_from_file (ctrl
, ctrl
->server_local
->keydesc
,
1268 grip
, &shadow_info
, CACHE_MODE_IGNORE
, &s_skey
);
1273 log_error ("changing a smartcard PIN is not yet supported\n");
1274 rc
= gpg_error (GPG_ERR_NOT_IMPLEMENTED
);
1277 rc
= agent_protect_and_store (ctrl
, s_skey
);
1280 xfree (ctrl
->server_local
->keydesc
);
1281 ctrl
->server_local
->keydesc
= NULL
;
1284 gcry_sexp_release (s_skey
);
1285 xfree (shadow_info
);
1287 log_error ("command passwd failed: %s\n", gpg_strerror (rc
));
1291 /* PRESET_PASSPHRASE <string_or_keygrip> <timeout> <hexstring>
1293 Set the cached passphrase/PIN for the key identified by the keygrip
1294 to passwd for the given time, where -1 means infinite and 0 means
1295 the default (currently only a timeout of -1 is allowed, which means
1296 to never expire it). If passwd is not provided, ask for it via the
1299 cmd_preset_passphrase (assuan_context_t ctx
, char *line
)
1302 char *grip_clear
= NULL
;
1303 char *passphrase
= NULL
;
1307 if (!opt
.allow_preset_passphrase
)
1308 return set_error (GPG_ERR_NOT_SUPPORTED
, "no --allow-preset-passphrase");
1311 while (*line
&& (*line
!= ' ' && *line
!= '\t'))
1314 return gpg_error (GPG_ERR_MISSING_VALUE
);
1317 while (*line
&& (*line
== ' ' || *line
== '\t'))
1320 /* Currently, only infinite timeouts are allowed. */
1322 if (line
[0] != '-' || line
[1] != '1')
1323 return gpg_error (GPG_ERR_NOT_IMPLEMENTED
);
1326 while (!(*line
!= ' ' && *line
!= '\t'))
1329 /* Syntax check the hexstring. */
1330 rc
= parse_hexstring (ctx
, line
, &len
);
1335 /* If there is a passphrase, use it. Currently, a passphrase is
1339 /* Do in-place conversion. */
1341 if (!hex2str (passphrase
, passphrase
, strlen (passphrase
)+1, NULL
))
1342 rc
= set_error (GPG_ERR_ASS_PARAMETER
, "invalid hexstring");
1345 rc
= set_error (GPG_ERR_NOT_IMPLEMENTED
, "passphrase is required");
1348 rc
= agent_put_cache (grip_clear
, CACHE_MODE_ANY
, passphrase
, ttl
);
1351 log_error ("command preset_passphrase failed: %s\n", gpg_strerror (rc
));
1357 /* SCD <commands to pass to the scdaemon>
1359 This is a general quote command to redirect everything to the
1362 cmd_scd (assuan_context_t ctx
, char *line
)
1364 ctrl_t ctrl
= assuan_get_pointer (ctx
);
1367 rc
= divert_generic_cmd (ctrl
, line
, ctx
);
1376 Return the value for KEY from the special environment as created by
1380 cmd_getval (assuan_context_t ctx
, char *line
)
1385 struct putval_item_s
*vl
;
1387 for (p
=line
; *p
== ' '; p
++)
1390 p
= strchr (key
, ' ');
1394 for (; *p
== ' '; p
++)
1397 return set_error (GPG_ERR_ASS_PARAMETER
, "too many arguments");
1400 return set_error (GPG_ERR_ASS_PARAMETER
, "no key given");
1403 for (vl
=putval_list
; vl
; vl
= vl
->next
)
1404 if ( !strcmp (vl
->d
, key
) )
1407 if (vl
) /* Got an entry. */
1408 rc
= assuan_send_data (ctx
, vl
->d
+vl
->off
, vl
->len
);
1410 return gpg_error (GPG_ERR_NO_DATA
);
1413 log_error ("command getval failed: %s\n", gpg_strerror (rc
));
1418 /* PUTVAL <key> [<percent_escaped_value>]
1420 The gpg-agent maintains a kind of environment which may be used to
1421 store key/value pairs in it, so that they can be retrieved later.
1422 This may be used by helper daemons to daemonize themself on
1423 invocation and register them with gpg-agent. Callers of the
1424 daemon's service may now first try connect to get the information
1425 for that service from gpg-agent through the GETVAL command and then
1426 try to connect to that daemon. Only if that fails they may start
1427 an own instance of the service daemon.
1429 KEY is an an arbitrary symbol with the same syntax rules as keys
1430 for shell environment variables. PERCENT_ESCAPED_VALUE is the
1431 corresponsing value; they should be similar to the values of
1432 envronment variables but gpg-agent does not enforce any
1433 restrictions. If that value is not given any value under that KEY
1434 is removed from this special environment.
1437 cmd_putval (assuan_context_t ctx
, char *line
)
1442 size_t valuelen
= 0;
1444 struct putval_item_s
*vl
, *vlprev
;
1446 for (p
=line
; *p
== ' '; p
++)
1449 p
= strchr (key
, ' ');
1453 for (; *p
== ' '; p
++)
1458 p
= strchr (value
, ' ');
1461 valuelen
= percent_plus_unescape_inplace (value
, 0);
1465 return set_error (GPG_ERR_ASS_PARAMETER
, "no key given");
1468 for (vl
=putval_list
,vlprev
=NULL
; vl
; vlprev
=vl
, vl
= vl
->next
)
1469 if ( !strcmp (vl
->d
, key
) )
1472 if (vl
) /* Delete old entry. */
1475 vlprev
->next
= vl
->next
;
1477 putval_list
= vl
->next
;
1481 if (valuelen
) /* Add entry. */
1483 vl
= xtrymalloc (sizeof *vl
+ strlen (key
) + valuelen
);
1485 rc
= gpg_error_from_syserror ();
1489 vl
->off
= strlen (key
) + 1;
1490 strcpy (vl
->d
, key
);
1491 memcpy (vl
->d
+ vl
->off
, value
, valuelen
);
1492 vl
->next
= putval_list
;
1498 log_error ("command putval failed: %s\n", gpg_strerror (rc
));
1507 Set startup TTY and X DISPLAY variables to the values of this
1508 session. This command is useful to pull future pinentries to
1509 another screen. It is only required because there is no way in the
1510 ssh-agent protocol to convey this information. */
1512 cmd_updatestartuptty (assuan_context_t ctx
, char *line
)
1514 ctrl_t ctrl
= assuan_get_pointer (ctx
);
1518 xfree (opt
.startup_display
); opt
.startup_display
= NULL
;
1519 xfree (opt
.startup_ttyname
); opt
.startup_ttyname
= NULL
;
1520 xfree (opt
.startup_ttytype
); opt
.startup_ttytype
= NULL
;
1521 xfree (opt
.startup_lc_ctype
); opt
.startup_lc_ctype
= NULL
;
1522 xfree (opt
.startup_lc_messages
); opt
.startup_lc_messages
= NULL
;
1523 xfree (opt
.startup_xauthority
); opt
.startup_xauthority
= NULL
;
1526 opt
.startup_display
= xtrystrdup (ctrl
->display
);
1528 opt
.startup_ttyname
= xtrystrdup (ctrl
->ttyname
);
1530 opt
.startup_ttytype
= xtrystrdup (ctrl
->ttytype
);
1532 opt
.startup_lc_ctype
= xtrystrdup (ctrl
->lc_ctype
);
1533 if (ctrl
->lc_messages
)
1534 opt
.startup_lc_messages
= xtrystrdup (ctrl
->lc_messages
);
1535 if (ctrl
->xauthority
)
1536 opt
.startup_xauthority
= xtrystrdup (ctrl
->xauthority
);
1537 if (ctrl
->pinentry_user_data
)
1538 opt
.startup_pinentry_user_data
= xtrystrdup (ctrl
->pinentry_user_data
);
1545 #ifdef HAVE_W32_SYSTEM
1548 Under Windows we start the agent on the fly. Thus it also make
1549 sense to allow a client to stop the agent. */
1551 cmd_killagent (assuan_context_t ctx
, char *line
)
1553 ctrl_t ctrl
= assuan_get_pointer (ctx
);
1557 ctrl
->server_local
->stopme
= 1;
1558 return gpg_error (GPG_ERR_EOF
);
1563 As signals are inconvenient under Windows, we provide this command
1564 to allow reloading of the configuration. */
1566 cmd_reloadagent (assuan_context_t ctx
, char *line
)
1571 agent_sighup_action ();
1574 #endif /*HAVE_W32_SYSTEM*/
1580 Multipurpose function to return a variety of information.
1581 Supported values for WHAT are:
1583 version - Return the version of the program.
1584 pid - Return the process id of the server.
1585 socket_name - Return the name of the socket.
1586 ssh_socket_name - Return the name of the ssh socket.
1587 scd_running - Return OK if the SCdaemon is already running.
1589 cmd_has_option CMD OPT
1590 - Returns OK if the command CMD implements the option OPT.
1593 cmd_getinfo (assuan_context_t ctx
, char *line
)
1597 if (!strcmp (line
, "version"))
1599 const char *s
= VERSION
;
1600 rc
= assuan_send_data (ctx
, s
, strlen (s
));
1602 else if (!strcmp (line
, "pid"))
1606 snprintf (numbuf
, sizeof numbuf
, "%lu", (unsigned long)getpid ());
1607 rc
= assuan_send_data (ctx
, numbuf
, strlen (numbuf
));
1609 else if (!strcmp (line
, "socket_name"))
1611 const char *s
= get_agent_socket_name ();
1614 rc
= assuan_send_data (ctx
, s
, strlen (s
));
1616 rc
= gpg_error (GPG_ERR_NO_DATA
);
1618 else if (!strcmp (line
, "ssh_socket_name"))
1620 const char *s
= get_agent_ssh_socket_name ();
1623 rc
= assuan_send_data (ctx
, s
, strlen (s
));
1625 rc
= gpg_error (GPG_ERR_NO_DATA
);
1627 else if (!strcmp (line
, "scd_running"))
1629 rc
= agent_scd_check_running ()? 0 : gpg_error (GPG_ERR_GENERAL
);
1631 else if (!strncmp (line
, "cmd_has_option", 14)
1632 && (line
[14] == ' ' || line
[14] == '\t' || !line
[14]))
1636 while (*line
== ' ' || *line
== '\t')
1639 rc
= gpg_error (GPG_ERR_MISSING_VALUE
);
1643 while (*line
&& (*line
!= ' ' && *line
!= '\t'))
1646 rc
= gpg_error (GPG_ERR_MISSING_VALUE
);
1650 while (*line
== ' ' || *line
== '\t')
1653 rc
= gpg_error (GPG_ERR_MISSING_VALUE
);
1657 if (!command_has_option (cmd
, cmdopt
))
1658 rc
= gpg_error (GPG_ERR_GENERAL
);
1664 rc
= set_error (GPG_ERR_ASS_PARAMETER
, "unknown value for WHAT");
1671 option_handler (assuan_context_t ctx
, const char *key
, const char *value
)
1673 ctrl_t ctrl
= assuan_get_pointer (ctx
);
1675 if (!strcmp (key
, "display"))
1678 xfree (ctrl
->display
);
1679 ctrl
->display
= xtrystrdup (value
);
1681 return out_of_core ();
1683 else if (!strcmp (key
, "ttyname"))
1688 xfree (ctrl
->ttyname
);
1689 ctrl
->ttyname
= xtrystrdup (value
);
1691 return out_of_core ();
1694 else if (!strcmp (key
, "ttytype"))
1699 xfree (ctrl
->ttytype
);
1700 ctrl
->ttytype
= xtrystrdup (value
);
1702 return out_of_core ();
1705 else if (!strcmp (key
, "lc-ctype"))
1708 xfree (ctrl
->lc_ctype
);
1709 ctrl
->lc_ctype
= xtrystrdup (value
);
1710 if (!ctrl
->lc_ctype
)
1711 return out_of_core ();
1713 else if (!strcmp (key
, "lc-messages"))
1715 if (ctrl
->lc_messages
)
1716 xfree (ctrl
->lc_messages
);
1717 ctrl
->lc_messages
= xtrystrdup (value
);
1718 if (!ctrl
->lc_messages
)
1719 return out_of_core ();
1721 else if (!strcmp (key
, "xauthority"))
1723 if (ctrl
->xauthority
)
1724 xfree (ctrl
->xauthority
);
1725 ctrl
->xauthority
= xtrystrdup (value
);
1726 if (!ctrl
->xauthority
)
1727 return out_of_core ();
1729 else if (!strcmp (key
, "pinentry-user-data"))
1731 if (ctrl
->pinentry_user_data
)
1732 xfree (ctrl
->pinentry_user_data
);
1733 ctrl
->pinentry_user_data
= xtrystrdup (value
);
1734 if (!ctrl
->pinentry_user_data
)
1735 return out_of_core ();
1737 else if (!strcmp (key
, "use-cache-for-signing"))
1738 ctrl
->server_local
->use_cache_for_signing
= *value
? atoi (value
) : 0;
1739 else if (!strcmp (key
, "allow-pinentry-notify"))
1740 ctrl
->server_local
->allow_pinentry_notify
= 1;
1742 return gpg_error (GPG_ERR_UNKNOWN_OPTION
);
1750 /* Called by libassuan after all commands. ERR is the error from the
1751 last assuan operation and not the one returned from the command. */
1753 post_cmd_notify (assuan_context_t ctx
, int err
)
1755 ctrl_t ctrl
= assuan_get_pointer (ctx
);
1759 /* Switch off any I/O monitor controlled logging pausing. */
1760 ctrl
->server_local
->pause_io_logging
= 0;
1764 /* This function is called by libassuan for all I/O. We use it here
1765 to disable logging for the GETEVENTCOUNTER commands. This is so
1766 that the debug output won't get cluttered by this primitive
1769 io_monitor (assuan_context_t ctx
, int direction
,
1770 const char *line
, size_t linelen
)
1772 ctrl_t ctrl
= assuan_get_pointer (ctx
);
1774 /* Note that we only check for the uppercase name. This allows to
1775 see the logging for debugging if using a non-upercase command
1777 if (ctx
&& !direction
1779 && !strncmp (line
, "GETEVENTCOUNTER", 15)
1780 && (linelen
== 15 || spacep (line
+15)))
1782 ctrl
->server_local
->pause_io_logging
= 1;
1785 return ctrl
->server_local
->pause_io_logging
? 1:0;
1789 /* Return true if the commznd CMD implements the option OPT. */
1791 command_has_option (const char *cmd
, const char *cmdopt
)
1793 if (!strcmp (cmd
, "GET_PASSPHRASE"))
1795 if (!strcmp (cmdopt
, "repeat"))
1803 /* Tell the assuan library about our commands */
1805 register_commands (assuan_context_t ctx
)
1809 int (*handler
)(assuan_context_t
, char *line
);
1811 { "GETEVENTCOUNTER",cmd_geteventcounter
},
1812 { "ISTRUSTED", cmd_istrusted
},
1813 { "HAVEKEY", cmd_havekey
},
1814 { "KEYINFO", cmd_keyinfo
},
1815 { "SIGKEY", cmd_sigkey
},
1816 { "SETKEY", cmd_sigkey
},
1817 { "SETKEYDESC", cmd_setkeydesc
},
1818 { "SETHASH", cmd_sethash
},
1819 { "PKSIGN", cmd_pksign
},
1820 { "PKDECRYPT", cmd_pkdecrypt
},
1821 { "GENKEY", cmd_genkey
},
1822 { "READKEY", cmd_readkey
},
1823 { "GET_PASSPHRASE", cmd_get_passphrase
},
1824 { "PRESET_PASSPHRASE", cmd_preset_passphrase
},
1825 { "CLEAR_PASSPHRASE", cmd_clear_passphrase
},
1826 { "GET_CONFIRMATION", cmd_get_confirmation
},
1827 { "LISTTRUSTED", cmd_listtrusted
},
1828 { "MARKTRUSTED", cmd_marktrusted
},
1829 { "LEARN", cmd_learn
},
1830 { "PASSWD", cmd_passwd
},
1834 { "GETVAL", cmd_getval
},
1835 { "PUTVAL", cmd_putval
},
1836 { "UPDATESTARTUPTTY", cmd_updatestartuptty
},
1837 #ifdef HAVE_W32_SYSTEM
1838 { "KILLAGENT", cmd_killagent
},
1839 { "RELOADAGENT", cmd_reloadagent
},
1841 { "GETINFO", cmd_getinfo
},
1846 for (i
=0; table
[i
].name
; i
++)
1848 rc
= assuan_register_command (ctx
, table
[i
].name
, table
[i
].handler
);
1852 #ifdef HAVE_ASSUAN_SET_IO_MONITOR
1853 assuan_register_post_cmd_notify (ctx
, post_cmd_notify
);
1855 assuan_register_reset_notify (ctx
, reset_notify
);
1856 assuan_register_option_handler (ctx
, option_handler
);
1861 /* Startup the server. If LISTEN_FD and FD is given as -1, this is a
1862 simple piper server, otherwise it is a regular server. CTRL is the
1863 control structure for this connection; it has only the basic
1866 start_command_handler (ctrl_t ctrl
, gnupg_fd_t listen_fd
, gnupg_fd_t fd
)
1869 assuan_context_t ctx
;
1871 if (listen_fd
== GNUPG_INVALID_FD
&& fd
== GNUPG_INVALID_FD
)
1877 rc
= assuan_init_pipe_server (&ctx
, filedes
);
1879 else if (listen_fd
!= GNUPG_INVALID_FD
)
1881 rc
= assuan_init_socket_server_ext (&ctx
, listen_fd
, 0);
1885 rc
= assuan_init_socket_server_ext (&ctx
, fd
, 2);
1889 log_error ("failed to initialize the server: %s\n",
1893 rc
= register_commands (ctx
);
1896 log_error ("failed to register commands with Assuan: %s\n",
1901 assuan_set_pointer (ctx
, ctrl
);
1902 ctrl
->server_local
= xcalloc (1, sizeof *ctrl
->server_local
);
1903 ctrl
->server_local
->assuan_ctx
= ctx
;
1904 ctrl
->server_local
->message_fd
= -1;
1905 ctrl
->server_local
->use_cache_for_signing
= 1;
1906 ctrl
->digest
.raw_value
= 0;
1909 assuan_set_log_stream (ctx
, log_get_stream ());
1911 #ifdef HAVE_ASSUAN_SET_IO_MONITOR
1912 assuan_set_io_monitor (ctx
, io_monitor
);
1917 rc
= assuan_accept (ctx
);
1918 if (gpg_err_code (rc
) == GPG_ERR_EOF
|| rc
== -1)
1924 log_info ("Assuan accept problem: %s\n", gpg_strerror (rc
));
1928 rc
= assuan_process (ctx
);
1931 log_info ("Assuan processing failed: %s\n", gpg_strerror (rc
));
1936 /* Reset the SCD if needed. */
1937 agent_reset_scd (ctrl
);
1939 /* Reset the pinentry (in case of popup messages). */
1940 agent_reset_query (ctrl
);
1943 assuan_deinit_server (ctx
);
1944 #ifdef HAVE_W32_SYSTEM
1945 if (ctrl
->server_local
->stopme
)
1948 xfree (ctrl
->server_local
);
1949 ctrl
->server_local
= NULL
;