Imported Upstream version 2.1.11
[platform/upstream/gpg2.git] / agent / command.c
1 /* command.c - gpg-agent command handler
2  * Copyright (C) 2001-2011 Free Software Foundation, Inc.
3  * Copyright (C) 2001-2013 Werner Koch
4  * Copyright (C) 2015 g10 Code GmbH.
5  *
6  * This file is part of GnuPG.
7  *
8  * GnuPG is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 3 of the License, or
11  * (at your option) any later version.
12  *
13  * GnuPG is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, see <http://www.gnu.org/licenses/>.
20  */
21
22 /* FIXME: we should not use the default assuan buffering but setup
23    some buffering in secure mempory to protect session keys etc. */
24
25 #include <config.h>
26
27 #include <errno.h>
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include <ctype.h>
32 #include <unistd.h>
33 #include <assert.h>
34 #include <sys/types.h>
35 #include <sys/stat.h>
36 #include <dirent.h>
37
38 #include "agent.h"
39 #include <assuan.h>
40 #include "i18n.h"
41 #include "cvt-openpgp.h"
42 #include "../common/ssh-utils.h"
43 #include "../common/asshelp.h"
44
45
46 /* Maximum allowed size of the inquired ciphertext.  */
47 #define MAXLEN_CIPHERTEXT 4096
48 /* Maximum allowed size of the key parameters.  */
49 #define MAXLEN_KEYPARAM 1024
50 /* Maximum allowed size of key data as used in inquiries (bytes). */
51 #define MAXLEN_KEYDATA 4096
52 /* The size of the import/export KEK key (in bytes).  */
53 #define KEYWRAP_KEYSIZE (128/8)
54
55 /* A shortcut to call assuan_set_error using an gpg_err_code_t and a
56    text string.  */
57 #define set_error(e,t) assuan_set_error (ctx, gpg_error (e), (t))
58
59 /* Check that the maximum digest length we support has at least the
60    length of the keygrip.  */
61 #if MAX_DIGEST_LEN < 20
62 #error MAX_DIGEST_LEN shorter than keygrip
63 #endif
64
65 /* Data used to associate an Assuan context with local server data.
66    This is this modules local part of the server_control_s struct.  */
67 struct server_local_s
68 {
69   /* Our Assuan context.  */
70   assuan_context_t assuan_ctx;
71
72   /* If this flag is true, the passphrase cache is used for signing
73      operations.  It defaults to true but may be set on a per
74      connection base.  The global option opt.ignore_cache_for_signing
75      takes precedence over this flag.  */
76   int use_cache_for_signing;
77
78   /* An allocated description for the next key operation.  This is
79      used if a pinnetry needs to be popped up.  */
80   char *keydesc;
81
82   /* Flags to suppress I/O logging during a command.  */
83   int pause_io_logging;
84
85   /* If this flags is set to true the agent will be terminated after
86      the end of the current session.  */
87   int stopme;
88
89   /* Flag indicating whether pinentry notifications shall be done. */
90   int allow_pinentry_notify;
91
92   /* Malloced KEK (Key-Encryption-Key) for the import_key command.  */
93   void *import_key;
94
95   /* Malloced KEK for the export_key command.  */
96   void *export_key;
97
98   /* Client is aware of the error code GPG_ERR_FULLY_CANCELED.  */
99   int allow_fully_canceled;
100
101   /* Last CACHE_NONCE sent as status (malloced).  */
102   char *last_cache_nonce;
103
104   /* Last PASSWD_NONCE sent as status (malloced). */
105   char *last_passwd_nonce;
106 };
107
108
109 /* An entry for the getval/putval commands. */
110 struct putval_item_s
111 {
112   struct putval_item_s *next;
113   size_t off;  /* Offset to the value into DATA.  */
114   size_t len;  /* Length of the value.  */
115   char d[1];   /* Key | Nul | value.  */
116 };
117
118
119 /* A list of key value pairs fpr the getval/putval commands.  */
120 static struct putval_item_s *putval_list;
121
122
123 \f
124 /* To help polling clients, we keep track of the number of certain
125    events.  This structure keeps those counters.  The counters are
126    integers and there should be no problem if they are overflowing as
127    callers need to check only whether a counter changed.  The actual
128    values are not meaningful. */
129 struct
130 {
131   /* Incremented if any of the other counters below changed. */
132   unsigned int any;
133
134   /* Incremented if a key is added or removed from the internal privat
135      key database. */
136   unsigned int key;
137
138   /* Incremented if a change of the card readers stati has been
139      detected. */
140   unsigned int card;
141
142 } eventcounter;
143
144
145 \f
146 /*  Local prototypes.  */
147 static int command_has_option (const char *cmd, const char *cmdopt);
148
149
150
151 \f
152 /* Release the memory buffer MB but first wipe out the used memory. */
153 static void
154 clear_outbuf (membuf_t *mb)
155 {
156   void *p;
157   size_t n;
158
159   p = get_membuf (mb, &n);
160   if (p)
161     {
162       wipememory (p, n);
163       xfree (p);
164     }
165 }
166
167
168 /* Write the content of memory buffer MB as assuan data to CTX and
169    wipe the buffer out afterwards. */
170 static gpg_error_t
171 write_and_clear_outbuf (assuan_context_t ctx, membuf_t *mb)
172 {
173   gpg_error_t ae;
174   void *p;
175   size_t n;
176
177   p = get_membuf (mb, &n);
178   if (!p)
179     return out_of_core ();
180   ae = assuan_send_data (ctx, p, n);
181   memset (p, 0, n);
182   xfree (p);
183   return ae;
184 }
185
186
187 /* Clear the nonces used to enable the passphrase cache for certain
188    multi-command command sequences.  */
189 static void
190 clear_nonce_cache (ctrl_t ctrl)
191 {
192   if (ctrl->server_local->last_cache_nonce)
193     {
194       agent_put_cache (ctrl->server_local->last_cache_nonce,
195                        CACHE_MODE_NONCE, NULL, 0);
196       xfree (ctrl->server_local->last_cache_nonce);
197       ctrl->server_local->last_cache_nonce = NULL;
198     }
199   if (ctrl->server_local->last_passwd_nonce)
200     {
201       agent_put_cache (ctrl->server_local->last_passwd_nonce,
202                        CACHE_MODE_NONCE, NULL, 0);
203       xfree (ctrl->server_local->last_passwd_nonce);
204       ctrl->server_local->last_passwd_nonce = NULL;
205     }
206 }
207
208
209 /* This function is called by Libassuan whenever thee client sends a
210    reset.  It has been registered similar to the other Assuan
211    commands.  */
212 static gpg_error_t
213 reset_notify (assuan_context_t ctx, char *line)
214 {
215   ctrl_t ctrl = assuan_get_pointer (ctx);
216
217   (void) line;
218
219   memset (ctrl->keygrip, 0, 20);
220   ctrl->have_keygrip = 0;
221   ctrl->digest.valuelen = 0;
222
223   xfree (ctrl->server_local->keydesc);
224   ctrl->server_local->keydesc = NULL;
225
226   clear_nonce_cache (ctrl);
227
228   return 0;
229 }
230
231
232 /* Skip over options in LINE.
233
234    Blanks after the options are also removed.  Options are indicated
235    by two leading dashes followed by a string consisting of non-space
236    characters.  The special option "--" indicates an explicit end of
237    options; all what follows will not be considered an option.  The
238    first no-option string also indicates the end of option parsing. */
239 static char *
240 skip_options (const char *line)
241 {
242   while (spacep (line))
243     line++;
244   while ( *line == '-' && line[1] == '-' )
245     {
246       while (*line && !spacep (line))
247         line++;
248       while (spacep (line))
249         line++;
250     }
251   return (char*)line;
252 }
253
254
255 /* Check whether the option NAME appears in LINE.  An example for a
256    line with options is:
257      --algo=42 --data foo bar
258    This function would then only return true if NAME is "data".  */
259 static int
260 has_option (const char *line, const char *name)
261 {
262   const char *s;
263   int n = strlen (name);
264
265   s = strstr (line, name);
266   if (s && s >= skip_options (line))
267     return 0;
268   return (s && (s == line || spacep (s-1)) && (!s[n] || spacep (s+n)));
269 }
270
271
272 /* Same as has_option but does only test for the name of the option
273    and ignores an argument, i.e. with NAME being "--hash" it would
274    return true for "--hash" as well as for "--hash=foo". */
275 static int
276 has_option_name (const char *line, const char *name)
277 {
278   const char *s;
279   int n = strlen (name);
280
281   s = strstr (line, name);
282   if (s && s >= skip_options (line))
283     return 0;
284   return (s && (s == line || spacep (s-1))
285           && (!s[n] || spacep (s+n) || s[n] == '='));
286 }
287
288
289 /* Return a pointer to the argument of the option with NAME.  If such
290    an option is not given, NULL is retruned. */
291 static char *
292 option_value (const char *line, const char *name)
293 {
294   char *s;
295   int n = strlen (name);
296
297   s = strstr (line, name);
298   if (s && s >= skip_options (line))
299     return NULL;
300   if (s && (s == line || spacep (s-1))
301       && s[n] && (spacep (s+n) || s[n] == '='))
302     {
303       s += n + 1;
304       s += strspn (s, " ");
305       if (*s && !spacep(s))
306         return s;
307     }
308   return NULL;
309 }
310
311
312 /* Replace all '+' by a blank in the string S. */
313 static void
314 plus_to_blank (char *s)
315 {
316   for (; *s; s++)
317     {
318       if (*s == '+')
319         *s = ' ';
320     }
321 }
322
323
324 /* Parse a hex string.  Return an Assuan error code or 0 on success and the
325    length of the parsed string in LEN. */
326 static int
327 parse_hexstring (assuan_context_t ctx, const char *string, size_t *len)
328 {
329   const char *p;
330   size_t n;
331
332   /* parse the hash value */
333   for (p=string, n=0; hexdigitp (p); p++, n++)
334     ;
335   if (*p != ' ' && *p != '\t' && *p)
336     return set_error (GPG_ERR_ASS_PARAMETER, "invalid hexstring");
337   if ((n&1))
338     return set_error (GPG_ERR_ASS_PARAMETER, "odd number of digits");
339   *len = n;
340   return 0;
341 }
342
343
344 /* Parse the keygrip in STRING into the provided buffer BUF.  BUF must
345    provide space for 20 bytes.  BUF is not changed if the function
346    returns an error. */
347 static int
348 parse_keygrip (assuan_context_t ctx, const char *string, unsigned char *buf)
349 {
350   int rc;
351   size_t n = 0;
352
353   rc = parse_hexstring (ctx, string, &n);
354   if (rc)
355     return rc;
356   n /= 2;
357   if (n != 20)
358     return set_error (GPG_ERR_ASS_PARAMETER, "invalid length of keygrip");
359
360   if (hex2bin (string, buf, 20) < 0)
361     return set_error (GPG_ERR_BUG, "hex2bin");
362
363   return 0;
364 }
365
366
367 /* Write an Assuan status line.  KEYWORD is the first item on the
368    status line.  The following arguments are all separated by a space
369    in the output.  The last argument must be a NULL.  Linefeeds and
370    carriage returns characters (which are not allowed in an Assuan
371    status line) are silently quoted in C-style.  */
372 gpg_error_t
373 agent_write_status (ctrl_t ctrl, const char *keyword, ...)
374 {
375   gpg_error_t err = 0;
376   va_list arg_ptr;
377   const char *text;
378   assuan_context_t ctx = ctrl->server_local->assuan_ctx;
379   char buf[950], *p;
380   size_t n;
381
382   va_start (arg_ptr, keyword);
383
384   p = buf;
385   n = 0;
386   while ( (text = va_arg (arg_ptr, const char *)) )
387     {
388       if (n)
389         {
390           *p++ = ' ';
391           n++;
392         }
393       for ( ; *text && n < DIM (buf)-3; n++, text++)
394         {
395           if (*text == '\n')
396             {
397               *p++ = '\\';
398               *p++ = 'n';
399             }
400           else if (*text == '\r')
401             {
402               *p++ = '\\';
403               *p++ = 'r';
404             }
405           else
406             *p++ = *text;
407         }
408     }
409   *p = 0;
410   err = assuan_write_status (ctx, keyword, buf);
411
412   va_end (arg_ptr);
413   return err;
414 }
415
416
417 /* This function is similar to print_assuan_status but takes a CTRL
418    arg instead of an assuan context as first argument.  */
419 gpg_error_t
420 agent_print_status (ctrl_t ctrl, const char *keyword, const char *format, ...)
421 {
422   gpg_error_t err;
423   va_list arg_ptr;
424   assuan_context_t ctx = ctrl->server_local->assuan_ctx;
425
426   va_start (arg_ptr, format);
427   err = vprint_assuan_status (ctx, keyword, format, arg_ptr);
428   va_end (arg_ptr);
429   return err;
430 }
431
432
433 /* Helper to notify the client about a launched Pinentry.  Because
434    that might disturb some older clients, this is only done if enabled
435    via an option.  Returns an gpg error code. */
436 gpg_error_t
437 agent_inq_pinentry_launched (ctrl_t ctrl, unsigned long pid)
438 {
439   char line[100];
440
441   if (!ctrl || !ctrl->server_local
442       || !ctrl->server_local->allow_pinentry_notify)
443     return 0;
444   snprintf (line, DIM(line)-1, "PINENTRY_LAUNCHED %lu", pid);
445   return assuan_inquire (ctrl->server_local->assuan_ctx, line, NULL, NULL, 0);
446 }
447
448
449 /* An agent progress callback for Libgcrypt.  This has been registered
450  * to be called via the progress dispatcher mechanism from
451  * gpg-agent.c  */
452 static void
453 progress_cb (ctrl_t ctrl, const char *what, int printchar,
454              int current, int total)
455 {
456   if (!ctrl || !ctrl->server_local || !ctrl->server_local->assuan_ctx)
457     ;
458   else if (printchar == '\n' && what && !strcmp (what, "primegen"))
459     agent_print_status (ctrl, "PROGRESS", "%.20s X 100 100", what);
460   else
461     agent_print_status (ctrl, "PROGRESS", "%.20s %c %d %d",
462                         what, printchar=='\n'?'X':printchar, current, total);
463 }
464
465
466 /* Helper to print a message while leaving a command.  */
467 static gpg_error_t
468 leave_cmd (assuan_context_t ctx, gpg_error_t err)
469 {
470   if (err)
471     {
472       const char *name = assuan_get_command_name (ctx);
473       if (!name)
474         name = "?";
475
476       /* Not all users of gpg-agent know about the fully canceled
477          error code; map it back if needed.  */
478       if (gpg_err_code (err) == GPG_ERR_FULLY_CANCELED)
479         {
480           ctrl_t ctrl = assuan_get_pointer (ctx);
481
482           if (!ctrl->server_local->allow_fully_canceled)
483             err = gpg_err_make (gpg_err_source (err), GPG_ERR_CANCELED);
484         }
485
486       /* Most code from common/ does not know the error source, thus
487          we fix this here.  */
488       if (gpg_err_source (err) == GPG_ERR_SOURCE_UNKNOWN)
489         err = gpg_err_make (GPG_ERR_SOURCE_DEFAULT, gpg_err_code (err));
490
491       if (gpg_err_source (err) == GPG_ERR_SOURCE_DEFAULT)
492         log_error ("command '%s' failed: %s\n", name,
493                    gpg_strerror (err));
494       else
495         log_error ("command '%s' failed: %s <%s>\n", name,
496                    gpg_strerror (err), gpg_strsource (err));
497     }
498   return err;
499 }
500
501
502 \f
503 static const char hlp_geteventcounter[] =
504   "GETEVENTCOUNTER\n"
505   "\n"
506   "Return a a status line named EVENTCOUNTER with the current values\n"
507   "of all event counters.  The values are decimal numbers in the range\n"
508   "0 to UINT_MAX and wrapping around to 0.  The actual values should\n"
509   "not be relied upon, they shall only be used to detect a change.\n"
510   "\n"
511   "The currently defined counters are:\n"
512   "\n"
513   "ANY  - Incremented with any change of any of the other counters.\n"
514   "KEY  - Incremented for added or removed private keys.\n"
515   "CARD - Incremented for changes of the card readers stati.";
516 static gpg_error_t
517 cmd_geteventcounter (assuan_context_t ctx, char *line)
518 {
519   ctrl_t ctrl = assuan_get_pointer (ctx);
520
521   (void)line;
522
523   if (ctrl->restricted)
524     return leave_cmd (ctx, gpg_error (GPG_ERR_FORBIDDEN));
525
526   return agent_print_status (ctrl, "EVENTCOUNTER", "%u %u %u",
527                              eventcounter.any,
528                              eventcounter.key,
529                              eventcounter.card);
530 }
531
532
533 /* This function should be called once for all key removals or
534    additions.  This function is assured not to do any context
535    switches. */
536 void
537 bump_key_eventcounter (void)
538 {
539   eventcounter.key++;
540   eventcounter.any++;
541 }
542
543
544 /* This function should be called for all card reader status
545    changes.  This function is assured not to do any context
546    switches. */
547 void
548 bump_card_eventcounter (void)
549 {
550   eventcounter.card++;
551   eventcounter.any++;
552 }
553
554
555
556 \f
557 static const char hlp_istrusted[] =
558   "ISTRUSTED <hexstring_with_fingerprint>\n"
559   "\n"
560   "Return OK when we have an entry with this fingerprint in our\n"
561   "trustlist";
562 static gpg_error_t
563 cmd_istrusted (assuan_context_t ctx, char *line)
564 {
565   ctrl_t ctrl = assuan_get_pointer (ctx);
566   int rc, n, i;
567   char *p;
568   char fpr[41];
569
570   /* Parse the fingerprint value. */
571   for (p=line,n=0; hexdigitp (p); p++, n++)
572     ;
573   if (*p || !(n == 40 || n == 32))
574     return set_error (GPG_ERR_ASS_PARAMETER, "invalid fingerprint");
575   i = 0;
576   if (n==32)
577     {
578       strcpy (fpr, "00000000");
579       i += 8;
580     }
581   for (p=line; i < 40; p++, i++)
582     fpr[i] = *p >= 'a'? (*p & 0xdf): *p;
583   fpr[i] = 0;
584   rc = agent_istrusted (ctrl, fpr, NULL);
585   if (!rc || gpg_err_code (rc) == GPG_ERR_NOT_TRUSTED)
586     return rc;
587   else if (rc == -1 || gpg_err_code (rc) == GPG_ERR_EOF )
588     return gpg_error (GPG_ERR_NOT_TRUSTED);
589   else
590     return leave_cmd (ctx, rc);
591 }
592
593
594 static const char hlp_listtrusted[] =
595   "LISTTRUSTED\n"
596   "\n"
597   "List all entries from the trustlist.";
598 static gpg_error_t
599 cmd_listtrusted (assuan_context_t ctx, char *line)
600 {
601   ctrl_t ctrl = assuan_get_pointer (ctx);
602   int rc;
603
604   (void)line;
605
606   if (ctrl->restricted)
607     return leave_cmd (ctx, gpg_error (GPG_ERR_FORBIDDEN));
608
609   rc = agent_listtrusted (ctx);
610   return leave_cmd (ctx, rc);
611 }
612
613
614 static const char hlp_martrusted[] =
615   "MARKTRUSTED <hexstring_with_fingerprint> <flag> <display_name>\n"
616   "\n"
617   "Store a new key in into the trustlist.";
618 static gpg_error_t
619 cmd_marktrusted (assuan_context_t ctx, char *line)
620 {
621   ctrl_t ctrl = assuan_get_pointer (ctx);
622   int rc, n, i;
623   char *p;
624   char fpr[41];
625   int flag;
626
627   if (ctrl->restricted)
628     return leave_cmd (ctx, gpg_error (GPG_ERR_FORBIDDEN));
629
630   /* parse the fingerprint value */
631   for (p=line,n=0; hexdigitp (p); p++, n++)
632     ;
633   if (!spacep (p) || !(n == 40 || n == 32))
634     return set_error (GPG_ERR_ASS_PARAMETER, "invalid fingerprint");
635   i = 0;
636   if (n==32)
637     {
638       strcpy (fpr, "00000000");
639       i += 8;
640     }
641   for (p=line; i < 40; p++, i++)
642     fpr[i] = *p >= 'a'? (*p & 0xdf): *p;
643   fpr[i] = 0;
644
645   while (spacep (p))
646     p++;
647   flag = *p++;
648   if ( (flag != 'S' && flag != 'P') || !spacep (p) )
649     return set_error (GPG_ERR_ASS_PARAMETER, "invalid flag - must be P or S");
650   while (spacep (p))
651     p++;
652
653   rc = agent_marktrusted (ctrl, p, fpr, flag);
654   return leave_cmd (ctx, rc);
655 }
656
657
658
659 \f
660 static const char hlp_havekey[] =
661   "HAVEKEY <hexstrings_with_keygrips>\n"
662   "\n"
663   "Return success if at least one of the secret keys with the given\n"
664   "keygrips is available.";
665 static gpg_error_t
666 cmd_havekey (assuan_context_t ctx, char *line)
667 {
668   gpg_error_t err;
669   unsigned char buf[20];
670
671   do
672     {
673       err = parse_keygrip (ctx, line, buf);
674       if (err)
675         return err;
676
677       if (!agent_key_available (buf))
678         return 0; /* Found.  */
679
680       while (*line && *line != ' ' && *line != '\t')
681         line++;
682       while (*line == ' ' || *line == '\t')
683         line++;
684     }
685   while (*line);
686
687   /* No leave_cmd() here because errors are expected and would clutter
688      the log.  */
689   return gpg_error (GPG_ERR_NO_SECKEY);
690 }
691
692
693 static const char hlp_sigkey[] =
694   "SIGKEY <hexstring_with_keygrip>\n"
695   "SETKEY <hexstring_with_keygrip>\n"
696   "\n"
697   "Set the  key used for a sign or decrypt operation.";
698 static gpg_error_t
699 cmd_sigkey (assuan_context_t ctx, char *line)
700 {
701   int rc;
702   ctrl_t ctrl = assuan_get_pointer (ctx);
703
704   rc = parse_keygrip (ctx, line, ctrl->keygrip);
705   if (rc)
706     return rc;
707   ctrl->have_keygrip = 1;
708   return 0;
709 }
710
711
712 static const char hlp_setkeydesc[] =
713   "SETKEYDESC plus_percent_escaped_string\n"
714   "\n"
715   "Set a description to be used for the next PKSIGN, PKDECRYPT, IMPORT_KEY\n"
716   "or EXPORT_KEY operation if this operation requires a passphrase.  If\n"
717   "this command is not used a default text will be used.  Note, that\n"
718   "this description implictly selects the label used for the entry\n"
719   "box; if the string contains the string PIN (which in general will\n"
720   "not be translated), \"PIN\" is used, otherwise the translation of\n"
721   "\"passphrase\" is used.  The description string should not contain\n"
722   "blanks unless they are percent or '+' escaped.\n"
723   "\n"
724   "The description is only valid for the next PKSIGN, PKDECRYPT,\n"
725   "IMPORT_KEY, EXPORT_KEY, or DELETE_KEY operation.";
726 static gpg_error_t
727 cmd_setkeydesc (assuan_context_t ctx, char *line)
728 {
729   ctrl_t ctrl = assuan_get_pointer (ctx);
730   char *desc, *p;
731
732   for (p=line; *p == ' '; p++)
733     ;
734   desc = p;
735   p = strchr (desc, ' ');
736   if (p)
737     *p = 0; /* We ignore any garbage; we might late use it for other args. */
738
739   if (!*desc)
740     return set_error (GPG_ERR_ASS_PARAMETER, "no description given");
741
742   /* Note, that we only need to replace the + characters and should
743      leave the other escaping in place because the escaped string is
744      send verbatim to the pinentry which does the unescaping (but not
745      the + replacing) */
746   plus_to_blank (desc);
747
748   xfree (ctrl->server_local->keydesc);
749
750   if (ctrl->restricted)
751     {
752       ctrl->server_local->keydesc = strconcat
753         ((ctrl->restricted == 2
754          ? _("Note: Request from the web browser.")
755          : _("Note: Request from a remote site.")  ), "%0A%0A", desc, NULL);
756     }
757   else
758     ctrl->server_local->keydesc = xtrystrdup (desc);
759   if (!ctrl->server_local->keydesc)
760     return out_of_core ();
761   return 0;
762 }
763
764
765 static const char hlp_sethash[] =
766   "SETHASH (--hash=<name>)|(<algonumber>) <hexstring>\n"
767   "\n"
768   "The client can use this command to tell the server about the data\n"
769   "(which usually is a hash) to be signed.";
770 static gpg_error_t
771 cmd_sethash (assuan_context_t ctx, char *line)
772 {
773   int rc;
774   size_t n;
775   char *p;
776   ctrl_t ctrl = assuan_get_pointer (ctx);
777   unsigned char *buf;
778   char *endp;
779   int algo;
780
781   /* Parse the alternative hash options which may be used instead of
782      the algo number.  */
783   if (has_option_name (line, "--hash"))
784     {
785       if (has_option (line, "--hash=sha1"))
786         algo = GCRY_MD_SHA1;
787       else if (has_option (line, "--hash=sha224"))
788         algo = GCRY_MD_SHA224;
789       else if (has_option (line, "--hash=sha256"))
790         algo = GCRY_MD_SHA256;
791       else if (has_option (line, "--hash=sha384"))
792         algo = GCRY_MD_SHA384;
793       else if (has_option (line, "--hash=sha512"))
794         algo = GCRY_MD_SHA512;
795       else if (has_option (line, "--hash=rmd160"))
796         algo = GCRY_MD_RMD160;
797       else if (has_option (line, "--hash=md5"))
798         algo = GCRY_MD_MD5;
799       else if (has_option (line, "--hash=tls-md5sha1"))
800         algo = MD_USER_TLS_MD5SHA1;
801       else
802         return set_error (GPG_ERR_ASS_PARAMETER, "invalid hash algorithm");
803     }
804   else
805     algo = 0;
806
807   line = skip_options (line);
808
809   if (!algo)
810     {
811       /* No hash option has been given: require an algo number instead  */
812       algo = (int)strtoul (line, &endp, 10);
813       for (line = endp; *line == ' ' || *line == '\t'; line++)
814         ;
815       if (!algo || gcry_md_test_algo (algo))
816         return set_error (GPG_ERR_UNSUPPORTED_ALGORITHM, NULL);
817     }
818   ctrl->digest.algo = algo;
819   ctrl->digest.raw_value = 0;
820
821   /* Parse the hash value. */
822   n = 0;
823   rc = parse_hexstring (ctx, line, &n);
824   if (rc)
825     return rc;
826   n /= 2;
827   if (algo == MD_USER_TLS_MD5SHA1 && n == 36)
828     ;
829   else if (n != 16 && n != 20 && n != 24
830            && n != 28 && n != 32 && n != 48 && n != 64)
831     return set_error (GPG_ERR_ASS_PARAMETER, "unsupported length of hash");
832
833   if (n > MAX_DIGEST_LEN)
834     return set_error (GPG_ERR_ASS_PARAMETER, "hash value to long");
835
836   buf = ctrl->digest.value;
837   ctrl->digest.valuelen = n;
838   for (p=line, n=0; n < ctrl->digest.valuelen; p += 2, n++)
839     buf[n] = xtoi_2 (p);
840   for (; n < ctrl->digest.valuelen; n++)
841     buf[n] = 0;
842   return 0;
843 }
844
845
846 static const char hlp_pksign[] =
847   "PKSIGN [<options>] [<cache_nonce>]\n"
848   "\n"
849   "Perform the actual sign operation.  Neither input nor output are\n"
850   "sensitive to eavesdropping.";
851 static gpg_error_t
852 cmd_pksign (assuan_context_t ctx, char *line)
853 {
854   int rc;
855   cache_mode_t cache_mode = CACHE_MODE_NORMAL;
856   ctrl_t ctrl = assuan_get_pointer (ctx);
857   membuf_t outbuf;
858   char *cache_nonce = NULL;
859   char *p;
860
861   line = skip_options (line);
862
863   p = line;
864   for (p=line; *p && *p != ' ' && *p != '\t'; p++)
865     ;
866   *p = '\0';
867   if (*line)
868     cache_nonce = xtrystrdup (line);
869
870   if (opt.ignore_cache_for_signing)
871     cache_mode = CACHE_MODE_IGNORE;
872   else if (!ctrl->server_local->use_cache_for_signing)
873     cache_mode = CACHE_MODE_IGNORE;
874
875   init_membuf (&outbuf, 512);
876
877   rc = agent_pksign (ctrl, cache_nonce, ctrl->server_local->keydesc,
878                      &outbuf, cache_mode);
879   if (rc)
880     clear_outbuf (&outbuf);
881   else
882     rc = write_and_clear_outbuf (ctx, &outbuf);
883
884   xfree (cache_nonce);
885   xfree (ctrl->server_local->keydesc);
886   ctrl->server_local->keydesc = NULL;
887   return leave_cmd (ctx, rc);
888 }
889
890
891 static const char hlp_pkdecrypt[] =
892   "PKDECRYPT [<options>]\n"
893   "\n"
894   "Perform the actual decrypt operation.  Input is not\n"
895   "sensitive to eavesdropping.";
896 static gpg_error_t
897 cmd_pkdecrypt (assuan_context_t ctx, char *line)
898 {
899   int rc;
900   ctrl_t ctrl = assuan_get_pointer (ctx);
901   unsigned char *value;
902   size_t valuelen;
903   membuf_t outbuf;
904   int padding;
905
906   (void)line;
907
908   /* First inquire the data to decrypt */
909   rc = print_assuan_status (ctx, "INQUIRE_MAXLEN", "%u", MAXLEN_CIPHERTEXT);
910   if (!rc)
911     rc = assuan_inquire (ctx, "CIPHERTEXT",
912                         &value, &valuelen, MAXLEN_CIPHERTEXT);
913   if (rc)
914     return rc;
915
916   init_membuf (&outbuf, 512);
917
918   rc = agent_pkdecrypt (ctrl, ctrl->server_local->keydesc,
919                         value, valuelen, &outbuf, &padding);
920   xfree (value);
921   if (rc)
922     clear_outbuf (&outbuf);
923   else
924     {
925       if (padding != -1)
926         rc = print_assuan_status (ctx, "PADDING", "%d", padding);
927       else
928         rc = 0;
929       if (!rc)
930         rc = write_and_clear_outbuf (ctx, &outbuf);
931     }
932   xfree (ctrl->server_local->keydesc);
933   ctrl->server_local->keydesc = NULL;
934   return leave_cmd (ctx, rc);
935 }
936
937
938 static const char hlp_genkey[] =
939   "GENKEY [--no-protection] [--preset] [--inq-passwd] [<cache_nonce>]\n"
940   "\n"
941   "Generate a new key, store the secret part and return the public\n"
942   "part.  Here is an example transaction:\n"
943   "\n"
944   "  C: GENKEY\n"
945   "  S: INQUIRE KEYPARAM\n"
946   "  C: D (genkey (rsa (nbits  2048)))\n"
947   "  C: END\n"
948   "  S: D (public-key\n"
949   "  S: D   (rsa (n 326487324683264) (e 10001)))\n"
950   "  S: OK key created\n"
951   "\n"
952   "When the --preset option is used the passphrase for the generated\n"
953   "key will be added to the cache.  When --inq-passwd is used an inquire\n"
954   "with the keyword NEWPASSWD is used to request the passphrase for the\n"
955   "new key.\n";
956 static gpg_error_t
957 cmd_genkey (assuan_context_t ctx, char *line)
958 {
959   ctrl_t ctrl = assuan_get_pointer (ctx);
960   int rc;
961   int no_protection;
962   unsigned char *value;
963   size_t valuelen;
964   unsigned char *newpasswd = NULL;
965   membuf_t outbuf;
966   char *cache_nonce = NULL;
967   int opt_preset;
968   int opt_inq_passwd;
969   size_t n;
970   char *p;
971
972   if (ctrl->restricted)
973     return leave_cmd (ctx, gpg_error (GPG_ERR_FORBIDDEN));
974
975   no_protection = has_option (line, "--no-protection");
976   opt_preset = has_option (line, "--preset");
977   opt_inq_passwd = has_option (line, "--inq-passwd");
978   line = skip_options (line);
979
980   p = line;
981   for (p=line; *p && *p != ' ' && *p != '\t'; p++)
982     ;
983   *p = '\0';
984   if (*line)
985     cache_nonce = xtrystrdup (line);
986
987   /* First inquire the parameters */
988   rc = print_assuan_status (ctx, "INQUIRE_MAXLEN", "%u", MAXLEN_KEYPARAM);
989   if (!rc)
990     rc = assuan_inquire (ctx, "KEYPARAM", &value, &valuelen, MAXLEN_KEYPARAM);
991   if (rc)
992     return rc;
993
994   init_membuf (&outbuf, 512);
995
996   /* If requested, ask for the password to be used for the key.  If
997      this is not used the regular Pinentry mechanism is used.  */
998   if (opt_inq_passwd && !no_protection)
999     {
1000       /* (N is used as a dummy) */
1001       assuan_begin_confidential (ctx);
1002       rc = assuan_inquire (ctx, "NEWPASSWD", &newpasswd, &n, 256);
1003       assuan_end_confidential (ctx);
1004       if (rc)
1005         goto leave;
1006       if (!*newpasswd)
1007         {
1008           /* Empty password given - switch to no-protection mode.  */
1009           xfree (newpasswd);
1010           newpasswd = NULL;
1011           no_protection = 1;
1012         }
1013
1014     }
1015
1016   rc = agent_genkey (ctrl, cache_nonce, (char*)value, valuelen, no_protection,
1017                      newpasswd, opt_preset, &outbuf);
1018
1019  leave:
1020   if (newpasswd)
1021     {
1022       /* Assuan_inquire does not allow us to read into secure memory
1023          thus we need to wipe it ourself.  */
1024       wipememory (newpasswd, strlen (newpasswd));
1025       xfree (newpasswd);
1026     }
1027   xfree (value);
1028   if (rc)
1029     clear_outbuf (&outbuf);
1030   else
1031     rc = write_and_clear_outbuf (ctx, &outbuf);
1032   xfree (cache_nonce);
1033   return leave_cmd (ctx, rc);
1034 }
1035
1036
1037
1038 \f
1039 static const char hlp_readkey[] =
1040   "READKEY <hexstring_with_keygrip>\n"
1041   "\n"
1042   "Return the public key for the given keygrip.";
1043 static gpg_error_t
1044 cmd_readkey (assuan_context_t ctx, char *line)
1045 {
1046   ctrl_t ctrl = assuan_get_pointer (ctx);
1047   int rc;
1048   unsigned char grip[20];
1049   gcry_sexp_t s_pkey = NULL;
1050
1051   if (ctrl->restricted)
1052     return leave_cmd (ctx, gpg_error (GPG_ERR_FORBIDDEN));
1053
1054   rc = parse_keygrip (ctx, line, grip);
1055   if (rc)
1056     return rc; /* Return immediately as this is already an Assuan error code.*/
1057
1058   rc = agent_public_key_from_file (ctrl, grip, &s_pkey);
1059   if (!rc)
1060     {
1061       size_t len;
1062       unsigned char *buf;
1063
1064       len = gcry_sexp_sprint (s_pkey, GCRYSEXP_FMT_CANON, NULL, 0);
1065       assert (len);
1066       buf = xtrymalloc (len);
1067       if (!buf)
1068         rc = gpg_error_from_syserror ();
1069       else
1070         {
1071           len = gcry_sexp_sprint (s_pkey, GCRYSEXP_FMT_CANON, buf, len);
1072           assert (len);
1073           rc = assuan_send_data (ctx, buf, len);
1074           xfree (buf);
1075         }
1076       gcry_sexp_release (s_pkey);
1077     }
1078
1079   return leave_cmd (ctx, rc);
1080 }
1081
1082
1083 \f
1084 static const char hlp_keyinfo[] =
1085   "KEYINFO [--[ssh-]list] [--data] [--ssh-fpr] [--with-ssh] <keygrip>\n"
1086   "\n"
1087   "Return information about the key specified by the KEYGRIP.  If the\n"
1088   "key is not available GPG_ERR_NOT_FOUND is returned.  If the option\n"
1089   "--list is given the keygrip is ignored and information about all\n"
1090   "available keys are returned.  If --ssh-list is given information\n"
1091   "about all keys listed in the sshcontrol are returned.  With --with-ssh\n"
1092   "information from sshcontrol is always added to the info. Unless --data\n"
1093   "is given, the information is returned as a status line using the format:\n"
1094   "\n"
1095   "  KEYINFO <keygrip> <type> <serialno> <idstr> <cached> <protection> <fpr>\n"
1096   "\n"
1097   "KEYGRIP is the keygrip.\n"
1098   "\n"
1099   "TYPE is describes the type of the key:\n"
1100   "    'D' - Regular key stored on disk,\n"
1101   "    'T' - Key is stored on a smartcard (token),\n"
1102   "    'X' - Unknown type,\n"
1103   "    '-' - Key is missing.\n"
1104   "\n"
1105   "SERIALNO is an ASCII string with the serial number of the\n"
1106   "         smartcard.  If the serial number is not known a single\n"
1107   "         dash '-' is used instead.\n"
1108   "\n"
1109   "IDSTR is the IDSTR used to distinguish keys on a smartcard.  If it\n"
1110   "      is not known a dash is used instead.\n"
1111   "\n"
1112   "CACHED is 1 if the passphrase for the key was found in the key cache.\n"
1113   "       If not, a '-' is used instead.\n"
1114   "\n"
1115   "PROTECTION describes the key protection type:\n"
1116   "    'P' - The key is protected with a passphrase,\n"
1117   "    'C' - The key is not protected,\n"
1118   "    '-' - Unknown protection.\n"
1119   "\n"
1120   "FPR returns the formatted ssh-style fingerprint of the key.  It is only\n"
1121   "    printed if the option --ssh-fpr has been used.  It defaults to '-'.\n"
1122   "\n"
1123   "TTL is the TTL in seconds for that key or '-' if n/a.\n"
1124   "\n"
1125   "FLAGS is a word consisting of one-letter flags:\n"
1126   "      'D' - The key has been disabled,\n"
1127   "      'S' - The key is listed in sshcontrol (requires --with-ssh),\n"
1128   "      'c' - Use of the key needs to be confirmed,\n"
1129   "      '-' - No flags given.\n"
1130   "\n"
1131   "More information may be added in the future.";
1132 static gpg_error_t
1133 do_one_keyinfo (ctrl_t ctrl, const unsigned char *grip, assuan_context_t ctx,
1134                 int data, int with_ssh_fpr, int in_ssh,
1135                 int ttl, int disabled, int confirm)
1136 {
1137   gpg_error_t err;
1138   char hexgrip[40+1];
1139   char *fpr = NULL;
1140   int keytype;
1141   unsigned char *shadow_info = NULL;
1142   char *serialno = NULL;
1143   char *idstr = NULL;
1144   const char *keytypestr;
1145   const char *cached;
1146   const char *protectionstr;
1147   char *pw;
1148   int missing_key = 0;
1149   char ttlbuf[20];
1150   char flagsbuf[5];
1151
1152   err = agent_key_info_from_file (ctrl, grip, &keytype, &shadow_info);
1153   if (err)
1154     {
1155       if (in_ssh && gpg_err_code (err) == GPG_ERR_NOT_FOUND)
1156         missing_key = 1;
1157       else
1158         goto leave;
1159     }
1160
1161   /* Reformat the grip so that we use uppercase as good style. */
1162   bin2hex (grip, 20, hexgrip);
1163
1164   if (ttl > 0)
1165     snprintf (ttlbuf, sizeof ttlbuf, "%d", ttl);
1166   else
1167     strcpy (ttlbuf, "-");
1168
1169   *flagsbuf = 0;
1170   if (disabled)
1171     strcat (flagsbuf, "D");
1172   if (in_ssh)
1173     strcat (flagsbuf, "S");
1174   if (confirm)
1175     strcat (flagsbuf, "c");
1176   if (!*flagsbuf)
1177     strcpy (flagsbuf, "-");
1178
1179
1180   if (missing_key)
1181     {
1182       protectionstr = "-"; keytypestr = "-";
1183     }
1184   else
1185     {
1186       switch (keytype)
1187         {
1188         case PRIVATE_KEY_CLEAR:
1189         case PRIVATE_KEY_OPENPGP_NONE:
1190           protectionstr = "C"; keytypestr = "D";
1191           break;
1192         case PRIVATE_KEY_PROTECTED: protectionstr = "P"; keytypestr = "D";
1193           break;
1194         case PRIVATE_KEY_SHADOWED: protectionstr = "-"; keytypestr = "T";
1195           break;
1196         default: protectionstr = "-"; keytypestr = "X";
1197           break;
1198         }
1199     }
1200
1201   /* Compute the ssh fingerprint if requested.  */
1202   if (with_ssh_fpr)
1203     {
1204       gcry_sexp_t key;
1205
1206       if (!agent_raw_key_from_file (ctrl, grip, &key))
1207         {
1208           ssh_get_fingerprint_string (key, &fpr);
1209           gcry_sexp_release (key);
1210         }
1211     }
1212
1213   /* Here we have a little race by doing the cache check separately
1214      from the retrieval function.  Given that the cache flag is only a
1215      hint, it should not really matter.  */
1216   pw = agent_get_cache (hexgrip, CACHE_MODE_NORMAL);
1217   cached = pw ? "1" : "-";
1218   xfree (pw);
1219
1220   if (shadow_info)
1221     {
1222       err = parse_shadow_info (shadow_info, &serialno, &idstr, NULL);
1223       if (err)
1224         goto leave;
1225     }
1226
1227   if (!data)
1228     err = agent_write_status (ctrl, "KEYINFO",
1229                               hexgrip,
1230                               keytypestr,
1231                               serialno? serialno : "-",
1232                               idstr? idstr : "-",
1233                               cached,
1234                               protectionstr,
1235                               fpr? fpr : "-",
1236                               ttlbuf,
1237                               flagsbuf,
1238                               NULL);
1239   else
1240     {
1241       char *string;
1242
1243       string = xtryasprintf ("%s %s %s %s %s %s %s %s %s\n",
1244                              hexgrip, keytypestr,
1245                              serialno? serialno : "-",
1246                              idstr? idstr : "-", cached, protectionstr,
1247                              fpr? fpr : "-",
1248                              ttlbuf,
1249                              flagsbuf);
1250       if (!string)
1251         err = gpg_error_from_syserror ();
1252       else
1253         err = assuan_send_data (ctx, string, strlen(string));
1254       xfree (string);
1255     }
1256
1257  leave:
1258   xfree (fpr);
1259   xfree (shadow_info);
1260   xfree (serialno);
1261   xfree (idstr);
1262   return err;
1263 }
1264
1265
1266 /* Entry int for the command KEYINFO.  This function handles the
1267    command option processing.  For details see hlp_keyinfo above.  */
1268 static gpg_error_t
1269 cmd_keyinfo (assuan_context_t ctx, char *line)
1270 {
1271   ctrl_t ctrl = assuan_get_pointer (ctx);
1272   int err;
1273   unsigned char grip[20];
1274   DIR *dir = NULL;
1275   int list_mode;
1276   int opt_data, opt_ssh_fpr, opt_with_ssh;
1277   ssh_control_file_t cf = NULL;
1278   char hexgrip[41];
1279   int disabled, ttl, confirm, is_ssh;
1280
1281   if (ctrl->restricted)
1282     return leave_cmd (ctx, gpg_error (GPG_ERR_FORBIDDEN));
1283
1284   if (has_option (line, "--ssh-list"))
1285     list_mode = 2;
1286   else
1287     list_mode = has_option (line, "--list");
1288   opt_data = has_option (line, "--data");
1289   opt_ssh_fpr = has_option (line, "--ssh-fpr");
1290   opt_with_ssh = has_option (line, "--with-ssh");
1291   line = skip_options (line);
1292
1293   if (opt_with_ssh || list_mode == 2)
1294     cf = ssh_open_control_file ();
1295
1296   if (list_mode == 2)
1297     {
1298       if (cf)
1299         {
1300           while (!ssh_read_control_file (cf, hexgrip,
1301                                          &disabled, &ttl, &confirm))
1302             {
1303               if (hex2bin (hexgrip, grip, 20) < 0 )
1304                 continue; /* Bad hex string.  */
1305               err = do_one_keyinfo (ctrl, grip, ctx, opt_data, opt_ssh_fpr, 1,
1306                                     ttl, disabled, confirm);
1307               if (err)
1308                 goto leave;
1309             }
1310         }
1311       err = 0;
1312     }
1313   else if (list_mode)
1314     {
1315       char *dirname;
1316       struct dirent *dir_entry;
1317
1318       dirname = make_filename_try (opt.homedir, GNUPG_PRIVATE_KEYS_DIR, NULL);
1319       if (!dirname)
1320         {
1321           err = gpg_error_from_syserror ();
1322           goto leave;
1323         }
1324       dir = opendir (dirname);
1325       if (!dir)
1326         {
1327           err = gpg_error_from_syserror ();
1328           xfree (dirname);
1329           goto leave;
1330         }
1331       xfree (dirname);
1332
1333       while ( (dir_entry = readdir (dir)) )
1334         {
1335           if (strlen (dir_entry->d_name) != 44
1336               || strcmp (dir_entry->d_name + 40, ".key"))
1337             continue;
1338           strncpy (hexgrip, dir_entry->d_name, 40);
1339           hexgrip[40] = 0;
1340
1341           if ( hex2bin (hexgrip, grip, 20) < 0 )
1342             continue; /* Bad hex string.  */
1343
1344           disabled = ttl = confirm = is_ssh = 0;
1345           if (opt_with_ssh)
1346             {
1347               err = ssh_search_control_file (cf, hexgrip,
1348                                              &disabled, &ttl, &confirm);
1349               if (!err)
1350                 is_ssh = 1;
1351               else if (gpg_err_code (err) != GPG_ERR_NOT_FOUND)
1352                 goto leave;
1353             }
1354
1355           err = do_one_keyinfo (ctrl, grip, ctx, opt_data, opt_ssh_fpr, is_ssh,
1356                                 ttl, disabled, confirm);
1357           if (err)
1358             goto leave;
1359         }
1360       err = 0;
1361     }
1362   else
1363     {
1364       err = parse_keygrip (ctx, line, grip);
1365       if (err)
1366         goto leave;
1367       disabled = ttl = confirm = is_ssh = 0;
1368       if (opt_with_ssh)
1369         {
1370           err = ssh_search_control_file (cf, line,
1371                                          &disabled, &ttl, &confirm);
1372           if (!err)
1373             is_ssh = 1;
1374           else if (gpg_err_code (err) != GPG_ERR_NOT_FOUND)
1375             goto leave;
1376         }
1377
1378       err = do_one_keyinfo (ctrl, grip, ctx, opt_data, opt_ssh_fpr, is_ssh,
1379                             ttl, disabled, confirm);
1380     }
1381
1382  leave:
1383   ssh_close_control_file (cf);
1384   if (dir)
1385     closedir (dir);
1386   if (err && gpg_err_code (err) != GPG_ERR_NOT_FOUND)
1387     leave_cmd (ctx, err);
1388   return err;
1389 }
1390
1391
1392 \f
1393 /* Helper for cmd_get_passphrase.  */
1394 static int
1395 send_back_passphrase (assuan_context_t ctx, int via_data, const char *pw)
1396 {
1397   size_t n;
1398   int rc;
1399
1400   assuan_begin_confidential (ctx);
1401   n = strlen (pw);
1402   if (via_data)
1403     rc = assuan_send_data (ctx, pw, n);
1404   else
1405     {
1406       char *p = xtrymalloc_secure (n*2+1);
1407       if (!p)
1408         rc = gpg_error_from_syserror ();
1409       else
1410         {
1411           bin2hex (pw, n, p);
1412           rc = assuan_set_okay_line (ctx, p);
1413           xfree (p);
1414         }
1415     }
1416   return rc;
1417 }
1418
1419
1420 static const char hlp_get_passphrase[] =
1421   "GET_PASSPHRASE [--data] [--check] [--no-ask] [--repeat[=N]]\n"
1422   "               [--qualitybar] <cache_id>\n"
1423   "               [<error_message> <prompt> <description>]\n"
1424   "\n"
1425   "This function is usually used to ask for a passphrase to be used\n"
1426   "for conventional encryption, but may also be used by programs which\n"
1427   "need specal handling of passphrases.  This command uses a syntax\n"
1428   "which helps clients to use the agent with minimum effort.  The\n"
1429   "agent either returns with an error or with a OK followed by the hex\n"
1430   "encoded passphrase.  Note that the length of the strings is\n"
1431   "implicitly limited by the maximum length of a command.\n"
1432   "\n"
1433   "If the option \"--data\" is used the passphrase is returned by usual\n"
1434   "data lines and not on the okay line.\n"
1435   "\n"
1436   "If the option \"--check\" is used the passphrase constraints checks as\n"
1437   "implemented by gpg-agent are applied.  A check is not done if the\n"
1438   "passphrase has been found in the cache.\n"
1439   "\n"
1440   "If the option \"--no-ask\" is used and the passphrase is not in the\n"
1441   "cache the user will not be asked to enter a passphrase but the error\n"
1442   "code GPG_ERR_NO_DATA is returned.  \n"
1443   "\n"
1444   "If the option \"--qualitybar\" is used a visual indication of the\n"
1445   "entered passphrase quality is shown.  (Unless no minimum passphrase\n"
1446   "length has been configured.)";
1447 static gpg_error_t
1448 cmd_get_passphrase (assuan_context_t ctx, char *line)
1449 {
1450   ctrl_t ctrl = assuan_get_pointer (ctx);
1451   int rc;
1452   char *pw;
1453   char *response;
1454   char *cacheid = NULL, *desc = NULL, *prompt = NULL, *errtext = NULL;
1455   const char *desc2 = _("Please re-enter this passphrase");
1456   char *p;
1457   int opt_data, opt_check, opt_no_ask, opt_qualbar;
1458   int opt_repeat = 0;
1459   char *entry_errtext = NULL;
1460
1461   if (ctrl->restricted)
1462     return leave_cmd (ctx, gpg_error (GPG_ERR_FORBIDDEN));
1463
1464   opt_data = has_option (line, "--data");
1465   opt_check = has_option (line, "--check");
1466   opt_no_ask = has_option (line, "--no-ask");
1467   if (has_option_name (line, "--repeat"))
1468     {
1469       p = option_value (line, "--repeat");
1470       if (p)
1471         opt_repeat = atoi (p);
1472       else
1473         opt_repeat = 1;
1474     }
1475   opt_qualbar = has_option (line, "--qualitybar");
1476   line = skip_options (line);
1477
1478   cacheid = line;
1479   p = strchr (cacheid, ' ');
1480   if (p)
1481     {
1482       *p++ = 0;
1483       while (*p == ' ')
1484         p++;
1485       errtext = p;
1486       p = strchr (errtext, ' ');
1487       if (p)
1488         {
1489           *p++ = 0;
1490           while (*p == ' ')
1491             p++;
1492           prompt = p;
1493           p = strchr (prompt, ' ');
1494           if (p)
1495             {
1496               *p++ = 0;
1497               while (*p == ' ')
1498                 p++;
1499               desc = p;
1500               p = strchr (desc, ' ');
1501               if (p)
1502                 *p = 0; /* Ignore trailing garbage. */
1503             }
1504         }
1505     }
1506   if (!*cacheid || strlen (cacheid) > 50)
1507     return set_error (GPG_ERR_ASS_PARAMETER, "invalid length of cacheID");
1508   if (!desc)
1509     return set_error (GPG_ERR_ASS_PARAMETER, "no description given");
1510
1511   if (!strcmp (cacheid, "X"))
1512     cacheid = NULL;
1513   if (!strcmp (errtext, "X"))
1514     errtext = NULL;
1515   if (!strcmp (prompt, "X"))
1516     prompt = NULL;
1517   if (!strcmp (desc, "X"))
1518     desc = NULL;
1519
1520   pw = cacheid ? agent_get_cache (cacheid, CACHE_MODE_USER) : NULL;
1521   if (pw)
1522     {
1523       rc = send_back_passphrase (ctx, opt_data, pw);
1524       xfree (pw);
1525     }
1526   else if (opt_no_ask)
1527     rc = gpg_error (GPG_ERR_NO_DATA);
1528   else
1529     {
1530       /* Note, that we only need to replace the + characters and
1531          should leave the other escaping in place because the escaped
1532          string is send verbatim to the pinentry which does the
1533          unescaping (but not the + replacing) */
1534       if (errtext)
1535         plus_to_blank (errtext);
1536       if (prompt)
1537         plus_to_blank (prompt);
1538       if (desc)
1539         plus_to_blank (desc);
1540
1541     next_try:
1542       rc = agent_get_passphrase (ctrl, &response, desc, prompt,
1543                                  entry_errtext? entry_errtext:errtext,
1544                                  opt_qualbar, cacheid, CACHE_MODE_USER);
1545       xfree (entry_errtext);
1546       entry_errtext = NULL;
1547       if (!rc)
1548         {
1549           int i;
1550
1551           if (opt_check
1552               && check_passphrase_constraints (ctrl, response, &entry_errtext))
1553             {
1554               xfree (response);
1555               goto next_try;
1556             }
1557           for (i = 0; i < opt_repeat; i++)
1558             {
1559               char *response2;
1560
1561               if (ctrl->pinentry_mode == PINENTRY_MODE_LOOPBACK)
1562                 break;
1563
1564               rc = agent_get_passphrase (ctrl, &response2, desc2, prompt,
1565                                          errtext, 0,
1566                                          cacheid, CACHE_MODE_USER);
1567               if (rc)
1568                 break;
1569               if (strcmp (response2, response))
1570                 {
1571                   xfree (response2);
1572                   xfree (response);
1573                   entry_errtext = try_percent_escape
1574                     (_("does not match - try again"), NULL);
1575                   if (!entry_errtext)
1576                     {
1577                       rc = gpg_error_from_syserror ();
1578                       break;
1579                     }
1580                   goto next_try;
1581                 }
1582               xfree (response2);
1583             }
1584           if (!rc)
1585             {
1586               if (cacheid)
1587                 agent_put_cache (cacheid, CACHE_MODE_USER, response, 0);
1588               rc = send_back_passphrase (ctx, opt_data, response);
1589             }
1590           xfree (response);
1591         }
1592     }
1593
1594   return leave_cmd (ctx, rc);
1595 }
1596
1597
1598 static const char hlp_clear_passphrase[] =
1599   "CLEAR_PASSPHRASE [--mode=normal] <cache_id>\n"
1600   "\n"
1601   "may be used to invalidate the cache entry for a passphrase.  The\n"
1602   "function returns with OK even when there is no cached passphrase.\n"
1603   "The --mode=normal option is used to clear an entry for a cacheid\n"
1604   "added by the agent.\n";
1605 static gpg_error_t
1606 cmd_clear_passphrase (assuan_context_t ctx, char *line)
1607 {
1608   ctrl_t ctrl = assuan_get_pointer (ctx);
1609   char *cacheid = NULL;
1610   char *p;
1611   int opt_normal;
1612
1613   if (ctrl->restricted)
1614     return leave_cmd (ctx, gpg_error (GPG_ERR_FORBIDDEN));
1615
1616   opt_normal = has_option (line, "--mode=normal");
1617   line = skip_options (line);
1618
1619   /* parse the stuff */
1620   for (p=line; *p == ' '; p++)
1621     ;
1622   cacheid = p;
1623   p = strchr (cacheid, ' ');
1624   if (p)
1625     *p = 0; /* ignore garbage */
1626   if (!*cacheid || strlen (cacheid) > 50)
1627     return set_error (GPG_ERR_ASS_PARAMETER, "invalid length of cacheID");
1628
1629   agent_put_cache (cacheid, opt_normal ? CACHE_MODE_NORMAL : CACHE_MODE_USER,
1630                    NULL, 0);
1631
1632   agent_clear_passphrase (ctrl, cacheid,
1633                           opt_normal ? CACHE_MODE_NORMAL : CACHE_MODE_USER);
1634
1635   return 0;
1636 }
1637
1638
1639 static const char hlp_get_confirmation[] =
1640   "GET_CONFIRMATION <description>\n"
1641   "\n"
1642   "This command may be used to ask for a simple confirmation.\n"
1643   "DESCRIPTION is displayed along with a Okay and Cancel button.  This\n"
1644   "command uses a syntax which helps clients to use the agent with\n"
1645   "minimum effort.  The agent either returns with an error or with a\n"
1646   "OK.  Note, that the length of DESCRIPTION is implicitly limited by\n"
1647   "the maximum length of a command. DESCRIPTION should not contain\n"
1648   "any spaces, those must be encoded either percent escaped or simply\n"
1649   "as '+'.";
1650 static gpg_error_t
1651 cmd_get_confirmation (assuan_context_t ctx, char *line)
1652 {
1653   ctrl_t ctrl = assuan_get_pointer (ctx);
1654   int rc;
1655   char *desc = NULL;
1656   char *p;
1657
1658   if (ctrl->restricted)
1659     return leave_cmd (ctx, gpg_error (GPG_ERR_FORBIDDEN));
1660
1661   /* parse the stuff */
1662   for (p=line; *p == ' '; p++)
1663     ;
1664   desc = p;
1665   p = strchr (desc, ' ');
1666   if (p)
1667     *p = 0; /* We ignore any garbage -may be later used for other args. */
1668
1669   if (!*desc)
1670     return set_error (GPG_ERR_ASS_PARAMETER, "no description given");
1671
1672   if (!strcmp (desc, "X"))
1673     desc = NULL;
1674
1675   /* Note, that we only need to replace the + characters and should
1676      leave the other escaping in place because the escaped string is
1677      send verbatim to the pinentry which does the unescaping (but not
1678      the + replacing) */
1679   if (desc)
1680     plus_to_blank (desc);
1681
1682   rc = agent_get_confirmation (ctrl, desc, NULL, NULL, 0);
1683   return leave_cmd (ctx, rc);
1684 }
1685
1686
1687 \f
1688 static const char hlp_learn[] =
1689   "LEARN [--send] [--sendinfo] [--force]\n"
1690   "\n"
1691   "Learn something about the currently inserted smartcard.  With\n"
1692   "--sendinfo information about the card is returned; with --send\n"
1693   "the available certificates are returned as D lines; with --force\n"
1694   "private key storage will be updated by the result.";
1695 static gpg_error_t
1696 cmd_learn (assuan_context_t ctx, char *line)
1697 {
1698   ctrl_t ctrl = assuan_get_pointer (ctx);
1699   gpg_error_t err;
1700   int send, sendinfo, force;
1701
1702   send = has_option (line, "--send");
1703   sendinfo = send? 1 : has_option (line, "--sendinfo");
1704   force = has_option (line, "--force");
1705
1706   if (ctrl->restricted)
1707     return leave_cmd (ctx, gpg_error (GPG_ERR_FORBIDDEN));
1708
1709   err = agent_handle_learn (ctrl, send, sendinfo? ctx : NULL, force);
1710   return leave_cmd (ctx, err);
1711 }
1712
1713
1714 \f
1715 static const char hlp_passwd[] =
1716   "PASSWD [--cache-nonce=<c>] [--passwd-nonce=<s>] [--preset]\n"
1717   "       [--verify] <hexkeygrip>\n"
1718   "\n"
1719   "Change the passphrase/PIN for the key identified by keygrip in LINE.  If\n"
1720   "--preset is used then the new passphrase will be added to the cache.\n"
1721   "If --verify is used the command asks for the passphrase and verifies\n"
1722   "that the passphrase valid.\n";
1723 static gpg_error_t
1724 cmd_passwd (assuan_context_t ctx, char *line)
1725 {
1726   ctrl_t ctrl = assuan_get_pointer (ctx);
1727   gpg_error_t err;
1728   int c;
1729   char *cache_nonce = NULL;
1730   char *passwd_nonce = NULL;
1731   unsigned char grip[20];
1732   gcry_sexp_t s_skey = NULL;
1733   unsigned char *shadow_info = NULL;
1734   char *passphrase = NULL;
1735   char *pend;
1736   int opt_preset, opt_verify;
1737
1738   if (ctrl->restricted)
1739     return leave_cmd (ctx, gpg_error (GPG_ERR_FORBIDDEN));
1740
1741   opt_preset = has_option (line, "--preset");
1742   cache_nonce = option_value (line, "--cache-nonce");
1743   opt_verify = has_option (line, "--verify");
1744   if (cache_nonce)
1745     {
1746       for (pend = cache_nonce; *pend && !spacep (pend); pend++)
1747         ;
1748       c = *pend;
1749       *pend = '\0';
1750       cache_nonce = xtrystrdup (cache_nonce);
1751       *pend = c;
1752       if (!cache_nonce)
1753         {
1754           err = gpg_error_from_syserror ();
1755           goto leave;
1756         }
1757     }
1758
1759   passwd_nonce = option_value (line, "--passwd-nonce");
1760   if (passwd_nonce)
1761     {
1762       for (pend = passwd_nonce; *pend && !spacep (pend); pend++)
1763         ;
1764       c = *pend;
1765       *pend = '\0';
1766       passwd_nonce = xtrystrdup (passwd_nonce);
1767       *pend = c;
1768       if (!passwd_nonce)
1769         {
1770           err = gpg_error_from_syserror ();
1771           goto leave;
1772         }
1773     }
1774
1775   line = skip_options (line);
1776
1777   err = parse_keygrip (ctx, line, grip);
1778   if (err)
1779     goto leave;
1780
1781   ctrl->in_passwd++;
1782   err = agent_key_from_file (ctrl,
1783                              opt_verify? NULL : cache_nonce,
1784                              ctrl->server_local->keydesc,
1785                              grip, &shadow_info, CACHE_MODE_IGNORE, NULL,
1786                              &s_skey, &passphrase);
1787   if (err)
1788     ;
1789   else if (shadow_info)
1790     {
1791       log_error ("changing a smartcard PIN is not yet supported\n");
1792       err = gpg_error (GPG_ERR_NOT_IMPLEMENTED);
1793     }
1794   else if (opt_verify)
1795     {
1796       /* All done.  */
1797     }
1798   else
1799     {
1800       char *newpass = NULL;
1801
1802       if (passwd_nonce)
1803         newpass = agent_get_cache (passwd_nonce, CACHE_MODE_NONCE);
1804       err = agent_protect_and_store (ctrl, s_skey, &newpass);
1805       if (!err && passphrase)
1806         {
1807           /* A passphrase existed on the old key and the change was
1808              successful.  Return a nonce for that old passphrase to
1809              let the caller try to unprotect the other subkeys with
1810              the same key.  */
1811           if (!cache_nonce)
1812             {
1813               char buf[12];
1814               gcry_create_nonce (buf, 12);
1815               cache_nonce = bin2hex (buf, 12, NULL);
1816             }
1817           if (cache_nonce
1818               && !agent_put_cache (cache_nonce, CACHE_MODE_NONCE,
1819                                    passphrase, CACHE_TTL_NONCE))
1820             {
1821               assuan_write_status (ctx, "CACHE_NONCE", cache_nonce);
1822               xfree (ctrl->server_local->last_cache_nonce);
1823               ctrl->server_local->last_cache_nonce = cache_nonce;
1824               cache_nonce = NULL;
1825             }
1826           if (newpass)
1827             {
1828               /* If we have a new passphrase (which might be empty) we
1829                  store it under a passwd nonce so that the caller may
1830                  send that nonce again to use it for another key. */
1831               if (!passwd_nonce)
1832                 {
1833                   char buf[12];
1834                   gcry_create_nonce (buf, 12);
1835                   passwd_nonce = bin2hex (buf, 12, NULL);
1836                 }
1837               if (passwd_nonce
1838                   && !agent_put_cache (passwd_nonce, CACHE_MODE_NONCE,
1839                                        newpass, CACHE_TTL_NONCE))
1840                 {
1841                   assuan_write_status (ctx, "PASSWD_NONCE", passwd_nonce);
1842                   xfree (ctrl->server_local->last_passwd_nonce);
1843                   ctrl->server_local->last_passwd_nonce = passwd_nonce;
1844                   passwd_nonce = NULL;
1845                 }
1846             }
1847         }
1848       if (!err && opt_preset)
1849         {
1850           char hexgrip[40+1];
1851           bin2hex(grip, 20, hexgrip);
1852           err = agent_put_cache (hexgrip, CACHE_MODE_ANY, newpass,
1853                                  ctrl->cache_ttl_opt_preset);
1854         }
1855       xfree (newpass);
1856     }
1857   ctrl->in_passwd--;
1858
1859   xfree (ctrl->server_local->keydesc);
1860   ctrl->server_local->keydesc = NULL;
1861
1862  leave:
1863   xfree (passphrase);
1864   gcry_sexp_release (s_skey);
1865   xfree (shadow_info);
1866   xfree (cache_nonce);
1867   return leave_cmd (ctx, err);
1868 }
1869
1870
1871 static const char hlp_preset_passphrase[] =
1872   "PRESET_PASSPHRASE [--inquire] <string_or_keygrip> <timeout> [<hexstring>]\n"
1873   "\n"
1874   "Set the cached passphrase/PIN for the key identified by the keygrip\n"
1875   "to passwd for the given time, where -1 means infinite and 0 means\n"
1876   "the default (currently only a timeout of -1 is allowed, which means\n"
1877   "to never expire it).  If passwd is not provided, ask for it via the\n"
1878   "pinentry module unless --inquire is passed in which case the passphrase\n"
1879   "is retrieved from the client via a server inquire.\n";
1880 static gpg_error_t
1881 cmd_preset_passphrase (assuan_context_t ctx, char *line)
1882 {
1883   ctrl_t ctrl = assuan_get_pointer (ctx);
1884   int rc;
1885   char *grip_clear = NULL;
1886   unsigned char *passphrase = NULL;
1887   int ttl;
1888   size_t len;
1889   int opt_inquire;
1890
1891   if (ctrl->restricted)
1892     return leave_cmd (ctx, gpg_error (GPG_ERR_FORBIDDEN));
1893
1894   if (!opt.allow_preset_passphrase)
1895     return set_error (GPG_ERR_NOT_SUPPORTED, "no --allow-preset-passphrase");
1896
1897   opt_inquire = has_option (line, "--inquire");
1898   line = skip_options (line);
1899   grip_clear = line;
1900   while (*line && (*line != ' ' && *line != '\t'))
1901     line++;
1902   if (!*line)
1903     return gpg_error (GPG_ERR_MISSING_VALUE);
1904   *line = '\0';
1905   line++;
1906   while (*line && (*line == ' ' || *line == '\t'))
1907     line++;
1908
1909   /* Currently, only infinite timeouts are allowed.  */
1910   ttl = -1;
1911   if (line[0] != '-' || line[1] != '1')
1912     return gpg_error (GPG_ERR_NOT_IMPLEMENTED);
1913   line++;
1914   line++;
1915   while (!(*line != ' ' && *line != '\t'))
1916     line++;
1917
1918   /* Syntax check the hexstring.  */
1919   len = 0;
1920   rc = parse_hexstring (ctx, line, &len);
1921   if (rc)
1922     return rc;
1923   line[len] = '\0';
1924
1925   /* If there is a passphrase, use it.  Currently, a passphrase is
1926      required.  */
1927   if (*line)
1928     {
1929       if (opt_inquire)
1930         {
1931           rc = set_error (GPG_ERR_ASS_PARAMETER,
1932                           "both --inquire and passphrase specified");
1933           goto leave;
1934         }
1935
1936       /* Do in-place conversion.  */
1937       passphrase = line;
1938       if (!hex2str (passphrase, passphrase, strlen (passphrase)+1, NULL))
1939         rc = set_error (GPG_ERR_ASS_PARAMETER, "invalid hexstring");
1940     }
1941   else if (opt_inquire)
1942     {
1943       /* Note that the passphrase will be truncated at any null byte and the
1944        * limit is 480 characters. */
1945       size_t maxlen = 480;
1946
1947       rc = print_assuan_status (ctx, "INQUIRE_MAXLEN", "%zu", maxlen);
1948       if (!rc)
1949         rc = assuan_inquire (ctx, "PASSPHRASE", &passphrase, &len, maxlen);
1950     }
1951   else
1952     rc = set_error (GPG_ERR_NOT_IMPLEMENTED, "passphrase is required");
1953
1954   if (!rc)
1955     {
1956       rc = agent_put_cache (grip_clear, CACHE_MODE_ANY, passphrase, ttl);
1957       if (opt_inquire)
1958         xfree (passphrase);
1959     }
1960
1961 leave:
1962   return leave_cmd (ctx, rc);
1963 }
1964
1965
1966 \f
1967 static const char hlp_scd[] =
1968   "SCD <commands to pass to the scdaemon>\n"
1969   " \n"
1970   "This is a general quote command to redirect everything to the\n"
1971   "SCdaemon.";
1972 static gpg_error_t
1973 cmd_scd (assuan_context_t ctx, char *line)
1974 {
1975   ctrl_t ctrl = assuan_get_pointer (ctx);
1976   int rc;
1977
1978   if (ctrl->restricted)
1979     return leave_cmd (ctx, gpg_error (GPG_ERR_FORBIDDEN));
1980
1981   rc = divert_generic_cmd (ctrl, line, ctx);
1982
1983   return rc;
1984 }
1985
1986
1987 \f
1988 static const char hlp_keywrap_key[] =
1989   "KEYWRAP_KEY [--clear] <mode>\n"
1990   "\n"
1991   "Return a key to wrap another key.  For now the key is returned\n"
1992   "verbatim and and thus makes not much sense because an eavesdropper on\n"
1993   "the gpg-agent connection will see the key as well as the wrapped key.\n"
1994   "However, this function may either be equipped with a public key\n"
1995   "mechanism or not used at all if the key is a pre-shared key.  In any\n"
1996   "case wrapping the import and export of keys is a requirement for\n"
1997   "certain cryptographic validations and thus useful.  The key persists\n"
1998   "until a RESET command but may be cleared using the option --clear.\n"
1999   "\n"
2000   "Supported modes are:\n"
2001   "  --import  - Return a key to import a key into gpg-agent\n"
2002   "  --export  - Return a key to export a key from gpg-agent";
2003 static gpg_error_t
2004 cmd_keywrap_key (assuan_context_t ctx, char *line)
2005 {
2006   ctrl_t ctrl = assuan_get_pointer (ctx);
2007   gpg_error_t err = 0;
2008   int clearopt = has_option (line, "--clear");
2009
2010   if (ctrl->restricted)
2011     return leave_cmd (ctx, gpg_error (GPG_ERR_FORBIDDEN));
2012
2013   assuan_begin_confidential (ctx);
2014   if (has_option (line, "--import"))
2015     {
2016       xfree (ctrl->server_local->import_key);
2017       if (clearopt)
2018         ctrl->server_local->import_key = NULL;
2019       else if (!(ctrl->server_local->import_key =
2020                  gcry_random_bytes (KEYWRAP_KEYSIZE, GCRY_STRONG_RANDOM)))
2021         err = gpg_error_from_syserror ();
2022       else
2023         err = assuan_send_data (ctx, ctrl->server_local->import_key,
2024                                 KEYWRAP_KEYSIZE);
2025     }
2026   else if (has_option (line, "--export"))
2027     {
2028       xfree (ctrl->server_local->export_key);
2029       if (clearopt)
2030         ctrl->server_local->export_key = NULL;
2031       else if (!(ctrl->server_local->export_key =
2032             gcry_random_bytes (KEYWRAP_KEYSIZE, GCRY_STRONG_RANDOM)))
2033         err = gpg_error_from_syserror ();
2034       else
2035         err = assuan_send_data (ctx, ctrl->server_local->export_key,
2036                                 KEYWRAP_KEYSIZE);
2037     }
2038   else
2039     err = set_error (GPG_ERR_ASS_PARAMETER, "unknown value for MODE");
2040   assuan_end_confidential (ctx);
2041
2042   return leave_cmd (ctx, err);
2043 }
2044
2045
2046 \f
2047 static const char hlp_import_key[] =
2048   "IMPORT_KEY [--unattended] [--force] [<cache_nonce>]\n"
2049   "\n"
2050   "Import a secret key into the key store.  The key is expected to be\n"
2051   "encrypted using the current session's key wrapping key (cf. command\n"
2052   "KEYWRAP_KEY) using the AESWRAP-128 algorithm.  This function takes\n"
2053   "no arguments but uses the inquiry \"KEYDATA\" to ask for the actual\n"
2054   "key data.  The unwrapped key must be a canonical S-expression.  The\n"
2055   "option --unattended tries to import the key as-is without any\n"
2056   "re-encryption.  Existing key can be overwritten with --force.";
2057 static gpg_error_t
2058 cmd_import_key (assuan_context_t ctx, char *line)
2059 {
2060   ctrl_t ctrl = assuan_get_pointer (ctx);
2061   gpg_error_t err;
2062   int opt_unattended;
2063   int force;
2064   unsigned char *wrappedkey = NULL;
2065   size_t wrappedkeylen;
2066   gcry_cipher_hd_t cipherhd = NULL;
2067   unsigned char *key = NULL;
2068   size_t keylen, realkeylen;
2069   char *passphrase = NULL;
2070   unsigned char *finalkey = NULL;
2071   size_t finalkeylen;
2072   unsigned char grip[20];
2073   gcry_sexp_t openpgp_sexp = NULL;
2074   char *cache_nonce = NULL;
2075   char *p;
2076
2077   if (ctrl->restricted)
2078     return leave_cmd (ctx, gpg_error (GPG_ERR_FORBIDDEN));
2079
2080   if (!ctrl->server_local->import_key)
2081     {
2082       err = gpg_error (GPG_ERR_MISSING_KEY);
2083       goto leave;
2084     }
2085
2086   opt_unattended = has_option (line, "--unattended");
2087   force = has_option (line, "--force");
2088   line = skip_options (line);
2089
2090   p = line;
2091   for (p=line; *p && *p != ' ' && *p != '\t'; p++)
2092     ;
2093   *p = '\0';
2094   if (*line)
2095     cache_nonce = xtrystrdup (line);
2096
2097   assuan_begin_confidential (ctx);
2098   err = assuan_inquire (ctx, "KEYDATA",
2099                         &wrappedkey, &wrappedkeylen, MAXLEN_KEYDATA);
2100   assuan_end_confidential (ctx);
2101   if (err)
2102     goto leave;
2103   if (wrappedkeylen < 24)
2104     {
2105       err = gpg_error (GPG_ERR_INV_LENGTH);
2106       goto leave;
2107     }
2108   keylen = wrappedkeylen - 8;
2109   key = xtrymalloc_secure (keylen);
2110   if (!key)
2111     {
2112       err = gpg_error_from_syserror ();
2113       goto leave;
2114     }
2115
2116   err = gcry_cipher_open (&cipherhd, GCRY_CIPHER_AES128,
2117                           GCRY_CIPHER_MODE_AESWRAP, 0);
2118   if (err)
2119     goto leave;
2120   err = gcry_cipher_setkey (cipherhd,
2121                             ctrl->server_local->import_key, KEYWRAP_KEYSIZE);
2122   if (err)
2123     goto leave;
2124   err = gcry_cipher_decrypt (cipherhd, key, keylen, wrappedkey, wrappedkeylen);
2125   if (err)
2126     goto leave;
2127   gcry_cipher_close (cipherhd);
2128   cipherhd = NULL;
2129   xfree (wrappedkey);
2130   wrappedkey = NULL;
2131
2132   realkeylen = gcry_sexp_canon_len (key, keylen, NULL, &err);
2133   if (!realkeylen)
2134     goto leave; /* Invalid canonical encoded S-expression.  */
2135
2136   err = keygrip_from_canon_sexp (key, realkeylen, grip);
2137   if (err)
2138     {
2139       /* This might be due to an unsupported S-expression format.
2140          Check whether this is openpgp-private-key and trigger that
2141          import code.  */
2142       if (!gcry_sexp_sscan (&openpgp_sexp, NULL, key, realkeylen))
2143         {
2144           const char *tag;
2145           size_t taglen;
2146
2147           tag = gcry_sexp_nth_data (openpgp_sexp, 0, &taglen);
2148           if (tag && taglen == 19 && !memcmp (tag, "openpgp-private-key", 19))
2149             ;
2150           else
2151             {
2152               gcry_sexp_release (openpgp_sexp);
2153               openpgp_sexp = NULL;
2154             }
2155         }
2156       if (!openpgp_sexp)
2157         goto leave; /* Note that ERR is still set.  */
2158     }
2159
2160
2161   if (openpgp_sexp)
2162     {
2163       /* In most cases the key is encrypted and thus the conversion
2164          function from the OpenPGP format to our internal format will
2165          ask for a passphrase.  That passphrase will be returned and
2166          used to protect the key using the same code as for regular
2167          key import. */
2168
2169       xfree (key);
2170       key = NULL;
2171       err = convert_from_openpgp (ctrl, openpgp_sexp, force, grip,
2172                                   ctrl->server_local->keydesc, cache_nonce,
2173                                   &key, opt_unattended? NULL : &passphrase);
2174       if (err)
2175         goto leave;
2176       realkeylen = gcry_sexp_canon_len (key, 0, NULL, &err);
2177       if (!realkeylen)
2178         goto leave; /* Invalid canonical encoded S-expression.  */
2179       if (passphrase)
2180         {
2181           assert (!opt_unattended);
2182           if (!cache_nonce)
2183             {
2184               char buf[12];
2185               gcry_create_nonce (buf, 12);
2186               cache_nonce = bin2hex (buf, 12, NULL);
2187             }
2188           if (cache_nonce
2189               && !agent_put_cache (cache_nonce, CACHE_MODE_NONCE,
2190                                    passphrase, CACHE_TTL_NONCE))
2191             assuan_write_status (ctx, "CACHE_NONCE", cache_nonce);
2192         }
2193     }
2194   else if (opt_unattended)
2195     {
2196       err = set_error (GPG_ERR_ASS_PARAMETER,
2197                        "\"--unattended\" may only be used with OpenPGP keys");
2198       goto leave;
2199     }
2200   else
2201     {
2202       if (!force && !agent_key_available (grip))
2203         err = gpg_error (GPG_ERR_EEXIST);
2204       else
2205         {
2206           char *prompt = xtryasprintf
2207             (_("Please enter the passphrase to protect the "
2208                "imported object within the %s system."), GNUPG_NAME);
2209           if (!prompt)
2210             err = gpg_error_from_syserror ();
2211           else
2212             err = agent_ask_new_passphrase (ctrl, prompt, &passphrase);
2213           xfree (prompt);
2214         }
2215       if (err)
2216         goto leave;
2217     }
2218
2219   if (passphrase)
2220     {
2221       err = agent_protect (key, passphrase, &finalkey, &finalkeylen,
2222                            ctrl->s2k_count);
2223       if (!err)
2224         err = agent_write_private_key (grip, finalkey, finalkeylen, force);
2225     }
2226   else
2227     err = agent_write_private_key (grip, key, realkeylen, force);
2228
2229  leave:
2230   gcry_sexp_release (openpgp_sexp);
2231   xfree (finalkey);
2232   xfree (passphrase);
2233   xfree (key);
2234   gcry_cipher_close (cipherhd);
2235   xfree (wrappedkey);
2236   xfree (cache_nonce);
2237   xfree (ctrl->server_local->keydesc);
2238   ctrl->server_local->keydesc = NULL;
2239   return leave_cmd (ctx, err);
2240 }
2241
2242
2243 \f
2244 static const char hlp_export_key[] =
2245   "EXPORT_KEY [--cache-nonce=<nonce>] [--openpgp] <hexstring_with_keygrip>\n"
2246   "\n"
2247   "Export a secret key from the key store.  The key will be encrypted\n"
2248   "using the current session's key wrapping key (cf. command KEYWRAP_KEY)\n"
2249   "using the AESWRAP-128 algorithm.  The caller needs to retrieve that key\n"
2250   "prior to using this command.  The function takes the keygrip as argument.\n";
2251 static gpg_error_t
2252 cmd_export_key (assuan_context_t ctx, char *line)
2253 {
2254   ctrl_t ctrl = assuan_get_pointer (ctx);
2255   gpg_error_t err;
2256   unsigned char grip[20];
2257   gcry_sexp_t s_skey = NULL;
2258   unsigned char *key = NULL;
2259   size_t keylen;
2260   gcry_cipher_hd_t cipherhd = NULL;
2261   unsigned char *wrappedkey = NULL;
2262   size_t wrappedkeylen;
2263   int openpgp;
2264   char *cache_nonce;
2265   char *passphrase = NULL;
2266   unsigned char *shadow_info = NULL;
2267   char *pend;
2268   int c;
2269
2270   if (ctrl->restricted)
2271     return leave_cmd (ctx, gpg_error (GPG_ERR_FORBIDDEN));
2272
2273   openpgp = has_option (line, "--openpgp");
2274   cache_nonce = option_value (line, "--cache-nonce");
2275   if (cache_nonce)
2276     {
2277       for (pend = cache_nonce; *pend && !spacep (pend); pend++)
2278         ;
2279       c = *pend;
2280       *pend = '\0';
2281       cache_nonce = xtrystrdup (cache_nonce);
2282       *pend = c;
2283       if (!cache_nonce)
2284         {
2285           err = gpg_error_from_syserror ();
2286           goto leave;
2287         }
2288     }
2289   line = skip_options (line);
2290
2291   if (!ctrl->server_local->export_key)
2292     {
2293       err = set_error (GPG_ERR_MISSING_KEY, "did you run KEYWRAP_KEY ?");
2294       goto leave;
2295     }
2296
2297   err = parse_keygrip (ctx, line, grip);
2298   if (err)
2299     goto leave;
2300
2301   if (agent_key_available (grip))
2302     {
2303       err = gpg_error (GPG_ERR_NO_SECKEY);
2304       goto leave;
2305     }
2306
2307   /* Get the key from the file.  With the openpgp flag we also ask for
2308      the passphrase so that we can use it to re-encrypt it.  */
2309   err = agent_key_from_file (ctrl, cache_nonce,
2310                              ctrl->server_local->keydesc, grip,
2311                              &shadow_info, CACHE_MODE_IGNORE, NULL, &s_skey,
2312                              openpgp ? &passphrase : NULL);
2313   if (err)
2314     goto leave;
2315   if (shadow_info)
2316     {
2317       /* Key is on a smartcard.  */
2318       err = gpg_error (GPG_ERR_UNUSABLE_SECKEY);
2319       goto leave;
2320     }
2321
2322   if (openpgp)
2323     {
2324       /* The openpgp option changes the key format into the OpenPGP
2325          key transfer format.  The result is already a padded
2326          canonical S-expression.  */
2327       if (!passphrase)
2328         {
2329           err = agent_ask_new_passphrase
2330             (ctrl, _("This key (or subkey) is not protected with a passphrase."
2331                      "  Please enter a new passphrase to export it."),
2332              &passphrase);
2333           if (err)
2334             goto leave;
2335         }
2336       err = convert_to_openpgp (ctrl, s_skey, passphrase, &key, &keylen);
2337       if (!err && passphrase)
2338         {
2339           if (!cache_nonce)
2340             {
2341               char buf[12];
2342               gcry_create_nonce (buf, 12);
2343               cache_nonce = bin2hex (buf, 12, NULL);
2344             }
2345           if (cache_nonce
2346               && !agent_put_cache (cache_nonce, CACHE_MODE_NONCE,
2347                                    passphrase, CACHE_TTL_NONCE))
2348             {
2349               assuan_write_status (ctx, "CACHE_NONCE", cache_nonce);
2350               xfree (ctrl->server_local->last_cache_nonce);
2351               ctrl->server_local->last_cache_nonce = cache_nonce;
2352               cache_nonce = NULL;
2353             }
2354         }
2355     }
2356   else
2357     {
2358       /* Convert into a canonical S-expression and wrap that.  */
2359       err = make_canon_sexp_pad (s_skey, 1, &key, &keylen);
2360     }
2361   if (err)
2362     goto leave;
2363   gcry_sexp_release (s_skey);
2364   s_skey = NULL;
2365
2366   err = gcry_cipher_open (&cipherhd, GCRY_CIPHER_AES128,
2367                           GCRY_CIPHER_MODE_AESWRAP, 0);
2368   if (err)
2369     goto leave;
2370   err = gcry_cipher_setkey (cipherhd,
2371                             ctrl->server_local->export_key, KEYWRAP_KEYSIZE);
2372   if (err)
2373     goto leave;
2374
2375   wrappedkeylen = keylen + 8;
2376   wrappedkey = xtrymalloc (wrappedkeylen);
2377   if (!wrappedkey)
2378     {
2379       err = gpg_error_from_syserror ();
2380       goto leave;
2381     }
2382
2383   err = gcry_cipher_encrypt (cipherhd, wrappedkey, wrappedkeylen, key, keylen);
2384   if (err)
2385     goto leave;
2386   xfree (key);
2387   key = NULL;
2388   gcry_cipher_close (cipherhd);
2389   cipherhd = NULL;
2390
2391   assuan_begin_confidential (ctx);
2392   err = assuan_send_data (ctx, wrappedkey, wrappedkeylen);
2393   assuan_end_confidential (ctx);
2394
2395
2396  leave:
2397   xfree (cache_nonce);
2398   xfree (passphrase);
2399   xfree (wrappedkey);
2400   gcry_cipher_close (cipherhd);
2401   xfree (key);
2402   gcry_sexp_release (s_skey);
2403   xfree (ctrl->server_local->keydesc);
2404   ctrl->server_local->keydesc = NULL;
2405   xfree (shadow_info);
2406
2407   return leave_cmd (ctx, err);
2408 }
2409
2410
2411 \f
2412 static const char hlp_delete_key[] =
2413   "DELETE_KEY [--force] <hexstring_with_keygrip>\n"
2414   "\n"
2415   "Delete a secret key from the key store.\n"
2416   "Unless --force is used the agent asks the user for confirmation.\n";
2417 static gpg_error_t
2418 cmd_delete_key (assuan_context_t ctx, char *line)
2419 {
2420   ctrl_t ctrl = assuan_get_pointer (ctx);
2421   gpg_error_t err;
2422   int force;
2423   unsigned char grip[20];
2424
2425   if (ctrl->restricted)
2426     return leave_cmd (ctx, gpg_error (GPG_ERR_FORBIDDEN));
2427
2428   force = has_option (line, "--force");
2429   line = skip_options (line);
2430
2431   err = parse_keygrip (ctx, line, grip);
2432   if (err)
2433     goto leave;
2434
2435   err = agent_delete_key (ctrl, ctrl->server_local->keydesc, grip, force );
2436   if (err)
2437     goto leave;
2438
2439  leave:
2440   xfree (ctrl->server_local->keydesc);
2441   ctrl->server_local->keydesc = NULL;
2442
2443   return leave_cmd (ctx, err);
2444 }
2445
2446
2447 \f
2448 static const char hlp_keytocard[] =
2449   "KEYTOCARD [--force] <hexstring_with_keygrip> <serialno> <id> <timestamp>\n"
2450   "\n";
2451 static gpg_error_t
2452 cmd_keytocard (assuan_context_t ctx, char *line)
2453 {
2454   ctrl_t ctrl = assuan_get_pointer (ctx);
2455   int force;
2456   gpg_error_t err = 0;
2457   unsigned char grip[20];
2458   gcry_sexp_t s_skey = NULL;
2459   unsigned char *keydata;
2460   size_t keydatalen, timestamplen;
2461   const char *serialno, *timestamp_str, *id;
2462   unsigned char *shadow_info = NULL;
2463   time_t timestamp;
2464
2465   if (ctrl->restricted)
2466     return leave_cmd (ctx, gpg_error (GPG_ERR_FORBIDDEN));
2467
2468   force = has_option (line, "--force");
2469   line = skip_options (line);
2470
2471   err = parse_keygrip (ctx, line, grip);
2472   if (err)
2473     return err;
2474
2475   if (agent_key_available (grip))
2476     return gpg_error (GPG_ERR_NO_SECKEY);
2477
2478   line += 40;
2479   while (*line && (*line == ' ' || *line == '\t'))
2480     line++;
2481   serialno = line;
2482   while (*line && (*line != ' ' && *line != '\t'))
2483     line++;
2484   if (!*line)
2485     return gpg_error (GPG_ERR_MISSING_VALUE);
2486   *line = '\0';
2487   line++;
2488   while (*line && (*line == ' ' || *line == '\t'))
2489     line++;
2490   id = line;
2491   while (*line && (*line != ' ' && *line != '\t'))
2492     line++;
2493   if (!*line)
2494     return gpg_error (GPG_ERR_MISSING_VALUE);
2495   *line = '\0';
2496   line++;
2497   while (*line && (*line == ' ' || *line == '\t'))
2498     line++;
2499   timestamp_str = line;
2500   while (*line && (*line != ' ' && *line != '\t'))
2501     line++;
2502   if (*line)
2503     *line = '\0';
2504   timestamplen = line - timestamp_str;
2505   if (timestamplen != 15)
2506     return gpg_error (GPG_ERR_INV_VALUE);
2507
2508   err = agent_key_from_file (ctrl, NULL, ctrl->server_local->keydesc, grip,
2509                              &shadow_info, CACHE_MODE_IGNORE, NULL,
2510                              &s_skey, NULL);
2511   if (err)
2512     {
2513       xfree (shadow_info);
2514       return err;
2515     }
2516   if (shadow_info)
2517     {
2518       /* Key is on a smartcard already.  */
2519       xfree (shadow_info);
2520       gcry_sexp_release (s_skey);
2521       return gpg_error (GPG_ERR_UNUSABLE_SECKEY);
2522     }
2523
2524   keydatalen =  gcry_sexp_sprint (s_skey, GCRYSEXP_FMT_CANON, NULL, 0);
2525   keydata = xtrymalloc_secure (keydatalen + 30);
2526   if (keydata == NULL)
2527     {
2528       gcry_sexp_release (s_skey);
2529       return gpg_error_from_syserror ();
2530     }
2531
2532   gcry_sexp_sprint (s_skey, GCRYSEXP_FMT_CANON, keydata, keydatalen);
2533   gcry_sexp_release (s_skey);
2534   keydatalen--;                 /* Decrement for last '\0'.  */
2535   /* Add timestamp "created-at" in the private key */
2536   timestamp = isotime2epoch (timestamp_str);
2537   snprintf (keydata+keydatalen-1, 30, "(10:created-at10:%010lu))", timestamp);
2538   keydatalen += 10 + 19 - 1;
2539   err = divert_writekey (ctrl, force, serialno, id, keydata, keydatalen);
2540   xfree (keydata);
2541
2542   return leave_cmd (ctx, err);
2543 }
2544
2545
2546 \f
2547 static const char hlp_getval[] =
2548   "GETVAL <key>\n"
2549   "\n"
2550   "Return the value for KEY from the special environment as created by\n"
2551   "PUTVAL.";
2552 static gpg_error_t
2553 cmd_getval (assuan_context_t ctx, char *line)
2554 {
2555   ctrl_t ctrl = assuan_get_pointer (ctx);
2556   int rc = 0;
2557   char *key = NULL;
2558   char *p;
2559   struct putval_item_s *vl;
2560
2561   if (ctrl->restricted)
2562     return leave_cmd (ctx, gpg_error (GPG_ERR_FORBIDDEN));
2563
2564   for (p=line; *p == ' '; p++)
2565     ;
2566   key = p;
2567   p = strchr (key, ' ');
2568   if (p)
2569     {
2570       *p++ = 0;
2571       for (; *p == ' '; p++)
2572         ;
2573       if (*p)
2574         return set_error (GPG_ERR_ASS_PARAMETER, "too many arguments");
2575     }
2576   if (!*key)
2577     return set_error (GPG_ERR_ASS_PARAMETER, "no key given");
2578
2579
2580   for (vl=putval_list; vl; vl = vl->next)
2581     if ( !strcmp (vl->d, key) )
2582       break;
2583
2584   if (vl) /* Got an entry. */
2585     rc = assuan_send_data (ctx, vl->d+vl->off, vl->len);
2586   else
2587     return gpg_error (GPG_ERR_NO_DATA);
2588
2589   return leave_cmd (ctx, rc);
2590 }
2591
2592
2593 static const char hlp_putval[] =
2594   "PUTVAL <key> [<percent_escaped_value>]\n"
2595   "\n"
2596   "The gpg-agent maintains a kind of environment which may be used to\n"
2597   "store key/value pairs in it, so that they can be retrieved later.\n"
2598   "This may be used by helper daemons to daemonize themself on\n"
2599   "invocation and register them with gpg-agent.  Callers of the\n"
2600   "daemon's service may now first try connect to get the information\n"
2601   "for that service from gpg-agent through the GETVAL command and then\n"
2602   "try to connect to that daemon.  Only if that fails they may start\n"
2603   "an own instance of the service daemon. \n"
2604   "\n"
2605   "KEY is an an arbitrary symbol with the same syntax rules as keys\n"
2606   "for shell environment variables.  PERCENT_ESCAPED_VALUE is the\n"
2607   "corresponding value; they should be similar to the values of\n"
2608   "envronment variables but gpg-agent does not enforce any\n"
2609   "restrictions.  If that value is not given any value under that KEY\n"
2610   "is removed from this special environment.";
2611 static gpg_error_t
2612 cmd_putval (assuan_context_t ctx, char *line)
2613 {
2614   ctrl_t ctrl = assuan_get_pointer (ctx);
2615   int rc = 0;
2616   char *key = NULL;
2617   char *value = NULL;
2618   size_t valuelen = 0;
2619   char *p;
2620   struct putval_item_s *vl, *vlprev;
2621
2622   if (ctrl->restricted)
2623     return leave_cmd (ctx, gpg_error (GPG_ERR_FORBIDDEN));
2624
2625   for (p=line; *p == ' '; p++)
2626     ;
2627   key = p;
2628   p = strchr (key, ' ');
2629   if (p)
2630     {
2631       *p++ = 0;
2632       for (; *p == ' '; p++)
2633         ;
2634       if (*p)
2635         {
2636           value = p;
2637           p = strchr (value, ' ');
2638           if (p)
2639             *p = 0;
2640           valuelen = percent_plus_unescape_inplace (value, 0);
2641         }
2642     }
2643   if (!*key)
2644     return set_error (GPG_ERR_ASS_PARAMETER, "no key given");
2645
2646
2647   for (vl=putval_list,vlprev=NULL; vl; vlprev=vl, vl = vl->next)
2648     if ( !strcmp (vl->d, key) )
2649       break;
2650
2651   if (vl) /* Delete old entry. */
2652     {
2653       if (vlprev)
2654         vlprev->next = vl->next;
2655       else
2656         putval_list = vl->next;
2657       xfree (vl);
2658     }
2659
2660   if (valuelen) /* Add entry. */
2661     {
2662       vl = xtrymalloc (sizeof *vl + strlen (key) + valuelen);
2663       if (!vl)
2664         rc = gpg_error_from_syserror ();
2665       else
2666         {
2667           vl->len = valuelen;
2668           vl->off = strlen (key) + 1;
2669           strcpy (vl->d, key);
2670           memcpy (vl->d + vl->off, value, valuelen);
2671           vl->next = putval_list;
2672           putval_list = vl;
2673         }
2674     }
2675
2676   return leave_cmd (ctx, rc);
2677 }
2678
2679
2680
2681 \f
2682 static const char hlp_updatestartuptty[] =
2683   "UPDATESTARTUPTTY\n"
2684   "\n"
2685   "Set startup TTY and X11 DISPLAY variables to the values of this\n"
2686   "session.  This command is useful to pull future pinentries to\n"
2687   "another screen.  It is only required because there is no way in the\n"
2688   "ssh-agent protocol to convey this information.";
2689 static gpg_error_t
2690 cmd_updatestartuptty (assuan_context_t ctx, char *line)
2691 {
2692   static const char *names[] =
2693     { "GPG_TTY", "DISPLAY", "TERM", "XAUTHORITY", "PINENTRY_USER_DATA", NULL };
2694   ctrl_t ctrl = assuan_get_pointer (ctx);
2695   gpg_error_t err = 0;
2696   session_env_t se;
2697   int idx;
2698   char *lc_ctype = NULL;
2699   char *lc_messages = NULL;
2700
2701   (void)line;
2702
2703   if (ctrl->restricted)
2704     return leave_cmd (ctx, gpg_error (GPG_ERR_FORBIDDEN));
2705
2706   se = session_env_new ();
2707   if (!se)
2708     err = gpg_error_from_syserror ();
2709
2710   for (idx=0; !err && names[idx]; idx++)
2711     {
2712       const char *value = session_env_getenv (ctrl->session_env, names[idx]);
2713       if (value)
2714         err = session_env_setenv (se, names[idx], value);
2715     }
2716
2717   if (!err && ctrl->lc_ctype)
2718     if (!(lc_ctype = xtrystrdup (ctrl->lc_ctype)))
2719       err = gpg_error_from_syserror ();
2720
2721   if (!err && ctrl->lc_messages)
2722     if (!(lc_messages = xtrystrdup (ctrl->lc_messages)))
2723       err = gpg_error_from_syserror ();
2724
2725   if (err)
2726     {
2727       session_env_release (se);
2728       xfree (lc_ctype);
2729       xfree (lc_messages);
2730     }
2731   else
2732     {
2733       session_env_release (opt.startup_env);
2734       opt.startup_env = se;
2735       xfree (opt.startup_lc_ctype);
2736       opt.startup_lc_ctype = lc_ctype;
2737       xfree (opt.startup_lc_messages);
2738       opt.startup_lc_messages = lc_messages;
2739     }
2740
2741   return err;
2742 }
2743
2744
2745 \f
2746 static const char hlp_killagent[] =
2747   "KILLAGENT\n"
2748   "\n"
2749   "Stop the agent.";
2750 static gpg_error_t
2751 cmd_killagent (assuan_context_t ctx, char *line)
2752 {
2753   ctrl_t ctrl = assuan_get_pointer (ctx);
2754
2755   (void)line;
2756
2757   if (ctrl->restricted)
2758     return leave_cmd (ctx, gpg_error (GPG_ERR_FORBIDDEN));
2759
2760   ctrl->server_local->stopme = 1;
2761   assuan_set_flag (ctx, ASSUAN_FORCE_CLOSE, 1);
2762   return 0;
2763 }
2764
2765
2766 static const char hlp_reloadagent[] =
2767   "RELOADAGENT\n"
2768   "\n"
2769   "This command is an alternative to SIGHUP\n"
2770   "to reload the configuration.";
2771 static gpg_error_t
2772 cmd_reloadagent (assuan_context_t ctx, char *line)
2773 {
2774   ctrl_t ctrl = assuan_get_pointer (ctx);
2775
2776   (void)line;
2777
2778   if (ctrl->restricted)
2779     return leave_cmd (ctx, gpg_error (GPG_ERR_FORBIDDEN));
2780
2781   agent_sighup_action ();
2782   return 0;
2783 }
2784
2785
2786 \f
2787 static const char hlp_getinfo[] =
2788   "GETINFO <what>\n"
2789   "\n"
2790   "Multipurpose function to return a variety of information.\n"
2791   "Supported values for WHAT are:\n"
2792   "\n"
2793   "  version     - Return the version of the program.\n"
2794   "  pid         - Return the process id of the server.\n"
2795   "  socket_name - Return the name of the socket.\n"
2796   "  ssh_socket_name - Return the name of the ssh socket.\n"
2797   "  scd_running - Return OK if the SCdaemon is already running.\n"
2798   "  s2k_count   - Return the calibrated S2K count.\n"
2799   "  std_env_names   - List the names of the standard environment.\n"
2800   "  std_session_env - List the standard session environment.\n"
2801   "  std_startup_env - List the standard startup environment.\n"
2802   "  cmd_has_option\n"
2803   "              - Returns OK if the command CMD implements the option OPT.\n"
2804   "  restricted  - Returns OK if the connection is in restricted mode.\n";
2805 static gpg_error_t
2806 cmd_getinfo (assuan_context_t ctx, char *line)
2807 {
2808   ctrl_t ctrl = assuan_get_pointer (ctx);
2809   int rc = 0;
2810
2811   if (!strcmp (line, "version"))
2812     {
2813       const char *s = VERSION;
2814       rc = assuan_send_data (ctx, s, strlen (s));
2815     }
2816   else if (!strncmp (line, "cmd_has_option", 14)
2817            && (line[14] == ' ' || line[14] == '\t' || !line[14]))
2818     {
2819       char *cmd, *cmdopt;
2820       line += 14;
2821       while (*line == ' ' || *line == '\t')
2822         line++;
2823       if (!*line)
2824         rc = gpg_error (GPG_ERR_MISSING_VALUE);
2825       else
2826         {
2827           cmd = line;
2828           while (*line && (*line != ' ' && *line != '\t'))
2829             line++;
2830           if (!*line)
2831             rc = gpg_error (GPG_ERR_MISSING_VALUE);
2832           else
2833             {
2834               *line++ = 0;
2835               while (*line == ' ' || *line == '\t')
2836                 line++;
2837               if (!*line)
2838                 rc = gpg_error (GPG_ERR_MISSING_VALUE);
2839               else
2840                 {
2841                   cmdopt = line;
2842                   if (!command_has_option (cmd, cmdopt))
2843                     rc = gpg_error (GPG_ERR_GENERAL);
2844                 }
2845             }
2846         }
2847     }
2848   else if (!strcmp (line, "s2k_count"))
2849     {
2850       char numbuf[50];
2851
2852       snprintf (numbuf, sizeof numbuf, "%lu", get_standard_s2k_count ());
2853       rc = assuan_send_data (ctx, numbuf, strlen (numbuf));
2854     }
2855   else if (!strcmp (line, "restricted"))
2856     {
2857       rc = ctrl->restricted? 0 : gpg_error (GPG_ERR_GENERAL);
2858     }
2859   else if (ctrl->restricted)
2860     {
2861       rc = gpg_error (GPG_ERR_FORBIDDEN);
2862     }
2863   /* All sub-commands below are not allowed in restricted mode.  */
2864   else if (!strcmp (line, "pid"))
2865     {
2866       char numbuf[50];
2867
2868       snprintf (numbuf, sizeof numbuf, "%lu", (unsigned long)getpid ());
2869       rc = assuan_send_data (ctx, numbuf, strlen (numbuf));
2870     }
2871   else if (!strcmp (line, "socket_name"))
2872     {
2873       const char *s = get_agent_socket_name ();
2874
2875       if (s)
2876         rc = assuan_send_data (ctx, s, strlen (s));
2877       else
2878         rc = gpg_error (GPG_ERR_NO_DATA);
2879     }
2880   else if (!strcmp (line, "ssh_socket_name"))
2881     {
2882       const char *s = get_agent_ssh_socket_name ();
2883
2884       if (s)
2885         rc = assuan_send_data (ctx, s, strlen (s));
2886       else
2887         rc = gpg_error (GPG_ERR_NO_DATA);
2888     }
2889   else if (!strcmp (line, "scd_running"))
2890     {
2891       rc = agent_scd_check_running ()? 0 : gpg_error (GPG_ERR_GENERAL);
2892     }
2893   else if (!strcmp (line, "std_env_names"))
2894     {
2895       int iterator;
2896       const char *name;
2897
2898       iterator = 0;
2899       while ((name = session_env_list_stdenvnames (&iterator, NULL)))
2900         {
2901           rc = assuan_send_data (ctx, name, strlen (name)+1);
2902           if (!rc)
2903             rc = assuan_send_data (ctx, NULL, 0);
2904           if (rc)
2905             break;
2906         }
2907     }
2908   else if (!strcmp (line, "std_session_env")
2909            || !strcmp (line, "std_startup_env"))
2910     {
2911       int iterator;
2912       const char *name, *value;
2913       char *string;
2914
2915       iterator = 0;
2916       while ((name = session_env_list_stdenvnames (&iterator, NULL)))
2917         {
2918           value = session_env_getenv_or_default
2919             (line[5] == 't'? opt.startup_env:ctrl->session_env, name, NULL);
2920           if (value)
2921             {
2922               string = xtryasprintf ("%s=%s", name, value);
2923               if (!string)
2924                 rc = gpg_error_from_syserror ();
2925               else
2926                 {
2927                   rc = assuan_send_data (ctx, string, strlen (string)+1);
2928                   if (!rc)
2929                     rc = assuan_send_data (ctx, NULL, 0);
2930                 }
2931               if (rc)
2932                 break;
2933             }
2934         }
2935     }
2936   else
2937     rc = set_error (GPG_ERR_ASS_PARAMETER, "unknown value for WHAT");
2938   return rc;
2939 }
2940
2941
2942 \f
2943 /* This function is called by Libassuan to parse the OPTION command.
2944    It has been registered similar to the other Assuan commands.  */
2945 static gpg_error_t
2946 option_handler (assuan_context_t ctx, const char *key, const char *value)
2947 {
2948   ctrl_t ctrl = assuan_get_pointer (ctx);
2949   gpg_error_t err = 0;
2950
2951   if (!strcmp (key, "agent-awareness"))
2952     {
2953       /* The value is a version string telling us of which agent
2954          version the caller is aware of.  */
2955       ctrl->server_local->allow_fully_canceled =
2956         gnupg_compare_version (value, "2.1.0");
2957     }
2958   else if (ctrl->restricted)
2959     {
2960       err = gpg_error (GPG_ERR_FORBIDDEN);
2961     }
2962   /* All options below are not allowed in restricted mode.  */
2963   else if (!strcmp (key, "putenv"))
2964     {
2965       /* Change the session's environment to be used for the
2966          Pinentry.  Valid values are:
2967           <NAME>            Delete envvar NAME
2968           <KEY>=            Set envvar NAME to the empty string
2969           <KEY>=<VALUE>     Set envvar NAME to VALUE
2970       */
2971       err = session_env_putenv (ctrl->session_env, value);
2972     }
2973   else if (!strcmp (key, "display"))
2974     {
2975       err = session_env_setenv (ctrl->session_env, "DISPLAY", value);
2976     }
2977   else if (!strcmp (key, "ttyname"))
2978     {
2979       if (!opt.keep_tty)
2980         err = session_env_setenv (ctrl->session_env, "GPG_TTY", value);
2981     }
2982   else if (!strcmp (key, "ttytype"))
2983     {
2984       if (!opt.keep_tty)
2985         err = session_env_setenv (ctrl->session_env, "TERM", value);
2986     }
2987   else if (!strcmp (key, "lc-ctype"))
2988     {
2989       if (ctrl->lc_ctype)
2990         xfree (ctrl->lc_ctype);
2991       ctrl->lc_ctype = xtrystrdup (value);
2992       if (!ctrl->lc_ctype)
2993         return out_of_core ();
2994     }
2995   else if (!strcmp (key, "lc-messages"))
2996     {
2997       if (ctrl->lc_messages)
2998         xfree (ctrl->lc_messages);
2999       ctrl->lc_messages = xtrystrdup (value);
3000       if (!ctrl->lc_messages)
3001         return out_of_core ();
3002     }
3003   else if (!strcmp (key, "xauthority"))
3004     {
3005       err = session_env_setenv (ctrl->session_env, "XAUTHORITY", value);
3006     }
3007   else if (!strcmp (key, "pinentry-user-data"))
3008     {
3009       err = session_env_setenv (ctrl->session_env, "PINENTRY_USER_DATA", value);
3010     }
3011   else if (!strcmp (key, "use-cache-for-signing"))
3012     ctrl->server_local->use_cache_for_signing = *value? atoi (value) : 0;
3013   else if (!strcmp (key, "allow-pinentry-notify"))
3014     ctrl->server_local->allow_pinentry_notify = 1;
3015   else if (!strcmp (key, "pinentry-mode"))
3016     {
3017       int tmp = parse_pinentry_mode (value);
3018       if (tmp == -1)
3019         err = gpg_error (GPG_ERR_INV_VALUE);
3020       else if (tmp == PINENTRY_MODE_LOOPBACK && !opt.allow_loopback_pinentry)
3021         err = gpg_error (GPG_ERR_NOT_SUPPORTED);
3022       else
3023         ctrl->pinentry_mode = tmp;
3024     }
3025   else if (!strcmp (key, "cache-ttl-opt-preset"))
3026     {
3027       ctrl->cache_ttl_opt_preset = *value? atoi (value) : 0;
3028     }
3029   else if (!strcmp (key, "s2k-count"))
3030     {
3031       ctrl->s2k_count = *value? strtoul(value, NULL, 10) : 0;
3032       if (ctrl->s2k_count && ctrl->s2k_count < 65536)
3033         {
3034           ctrl->s2k_count = 0;
3035         }
3036     }
3037   else
3038     err = gpg_error (GPG_ERR_UNKNOWN_OPTION);
3039
3040   return err;
3041 }
3042
3043
3044
3045 \f
3046 /* Called by libassuan after all commands. ERR is the error from the
3047    last assuan operation and not the one returned from the command. */
3048 static void
3049 post_cmd_notify (assuan_context_t ctx, gpg_error_t err)
3050 {
3051   ctrl_t ctrl = assuan_get_pointer (ctx);
3052
3053   (void)err;
3054
3055   /* Switch off any I/O monitor controlled logging pausing. */
3056   ctrl->server_local->pause_io_logging = 0;
3057 }
3058
3059
3060 /* This function is called by libassuan for all I/O.  We use it here
3061    to disable logging for the GETEVENTCOUNTER commands.  This is so
3062    that the debug output won't get cluttered by this primitive
3063    command.  */
3064 static unsigned int
3065 io_monitor (assuan_context_t ctx, void *hook, int direction,
3066             const char *line, size_t linelen)
3067 {
3068   ctrl_t ctrl = assuan_get_pointer (ctx);
3069
3070   (void) hook;
3071
3072   /* Note that we only check for the uppercase name.  This allows to
3073      see the logging for debugging if using a non-upercase command
3074      name. */
3075   if (ctx && direction == ASSUAN_IO_FROM_PEER
3076       && linelen >= 15
3077       && !strncmp (line, "GETEVENTCOUNTER", 15)
3078       && (linelen == 15 || spacep (line+15)))
3079     {
3080       ctrl->server_local->pause_io_logging = 1;
3081     }
3082
3083   return ctrl->server_local->pause_io_logging? ASSUAN_IO_MONITOR_NOLOG : 0;
3084 }
3085
3086
3087 /* Return true if the command CMD implements the option OPT.  */
3088 static int
3089 command_has_option (const char *cmd, const char *cmdopt)
3090 {
3091   if (!strcmp (cmd, "GET_PASSPHRASE"))
3092     {
3093       if (!strcmp (cmdopt, "repeat"))
3094           return 1;
3095     }
3096
3097   return 0;
3098 }
3099
3100
3101 /* Tell Libassuan about our commands.  Also register the other Assuan
3102    handlers. */
3103 static int
3104 register_commands (assuan_context_t ctx)
3105 {
3106   static struct {
3107     const char *name;
3108     assuan_handler_t handler;
3109     const char * const help;
3110   } table[] = {
3111     { "GETEVENTCOUNTER",cmd_geteventcounter, hlp_geteventcounter },
3112     { "ISTRUSTED",      cmd_istrusted, hlp_istrusted },
3113     { "HAVEKEY",        cmd_havekey,   hlp_havekey },
3114     { "KEYINFO",        cmd_keyinfo,   hlp_keyinfo },
3115     { "SIGKEY",         cmd_sigkey,    hlp_sigkey },
3116     { "SETKEY",         cmd_sigkey,    hlp_sigkey },
3117     { "SETKEYDESC",     cmd_setkeydesc,hlp_setkeydesc },
3118     { "SETHASH",        cmd_sethash,   hlp_sethash },
3119     { "PKSIGN",         cmd_pksign,    hlp_pksign },
3120     { "PKDECRYPT",      cmd_pkdecrypt, hlp_pkdecrypt },
3121     { "GENKEY",         cmd_genkey,    hlp_genkey },
3122     { "READKEY",        cmd_readkey,   hlp_readkey },
3123     { "GET_PASSPHRASE", cmd_get_passphrase, hlp_get_passphrase },
3124     { "PRESET_PASSPHRASE", cmd_preset_passphrase, hlp_preset_passphrase },
3125     { "CLEAR_PASSPHRASE", cmd_clear_passphrase,   hlp_clear_passphrase },
3126     { "GET_CONFIRMATION", cmd_get_confirmation,   hlp_get_confirmation },
3127     { "LISTTRUSTED",    cmd_listtrusted, hlp_listtrusted },
3128     { "MARKTRUSTED",    cmd_marktrusted, hlp_martrusted },
3129     { "LEARN",          cmd_learn,     hlp_learn },
3130     { "PASSWD",         cmd_passwd,    hlp_passwd },
3131     { "INPUT",          NULL },
3132     { "OUTPUT",         NULL },
3133     { "SCD",            cmd_scd,       hlp_scd },
3134     { "KEYWRAP_KEY",    cmd_keywrap_key, hlp_keywrap_key },
3135     { "IMPORT_KEY",     cmd_import_key, hlp_import_key },
3136     { "EXPORT_KEY",     cmd_export_key, hlp_export_key },
3137     { "DELETE_KEY",     cmd_delete_key, hlp_delete_key },
3138     { "GETVAL",         cmd_getval,    hlp_getval },
3139     { "PUTVAL",         cmd_putval,    hlp_putval },
3140     { "UPDATESTARTUPTTY",  cmd_updatestartuptty, hlp_updatestartuptty },
3141     { "KILLAGENT",      cmd_killagent,  hlp_killagent },
3142     { "RELOADAGENT",    cmd_reloadagent,hlp_reloadagent },
3143     { "GETINFO",        cmd_getinfo,   hlp_getinfo },
3144     { "KEYTOCARD",      cmd_keytocard, hlp_keytocard },
3145     { NULL }
3146   };
3147   int i, rc;
3148
3149   for (i=0; table[i].name; i++)
3150     {
3151       rc = assuan_register_command (ctx, table[i].name, table[i].handler,
3152                                     table[i].help);
3153       if (rc)
3154         return rc;
3155     }
3156   assuan_register_post_cmd_notify (ctx, post_cmd_notify);
3157   assuan_register_reset_notify (ctx, reset_notify);
3158   assuan_register_option_handler (ctx, option_handler);
3159   return 0;
3160 }
3161
3162
3163 /* Startup the server.  If LISTEN_FD and FD is given as -1, this is a
3164    simple piper server, otherwise it is a regular server.  CTRL is the
3165    control structure for this connection; it has only the basic
3166    intialization. */
3167 void
3168 start_command_handler (ctrl_t ctrl, gnupg_fd_t listen_fd, gnupg_fd_t fd)
3169 {
3170   int rc;
3171   assuan_context_t ctx = NULL;
3172
3173   if (ctrl->restricted)
3174     {
3175       if (agent_copy_startup_env (ctrl))
3176         return;
3177     }
3178
3179   rc = assuan_new (&ctx);
3180   if (rc)
3181     {
3182       log_error ("failed to allocate assuan context: %s\n", gpg_strerror (rc));
3183       agent_exit (2);
3184     }
3185
3186   if (listen_fd == GNUPG_INVALID_FD && fd == GNUPG_INVALID_FD)
3187     {
3188       assuan_fd_t filedes[2];
3189
3190       filedes[0] = assuan_fdopen (0);
3191       filedes[1] = assuan_fdopen (1);
3192       rc = assuan_init_pipe_server (ctx, filedes);
3193     }
3194   else if (listen_fd != GNUPG_INVALID_FD)
3195     {
3196       rc = assuan_init_socket_server (ctx, listen_fd, 0);
3197       /* FIXME: Need to call assuan_sock_set_nonce for Windows.  But
3198          this branch is currently not used.  */
3199     }
3200   else
3201     {
3202       rc = assuan_init_socket_server (ctx, fd, ASSUAN_SOCKET_SERVER_ACCEPTED);
3203     }
3204   if (rc)
3205     {
3206       log_error ("failed to initialize the server: %s\n",
3207                  gpg_strerror(rc));
3208       agent_exit (2);
3209     }
3210   rc = register_commands (ctx);
3211   if (rc)
3212     {
3213       log_error ("failed to register commands with Assuan: %s\n",
3214                  gpg_strerror(rc));
3215       agent_exit (2);
3216     }
3217
3218   assuan_set_pointer (ctx, ctrl);
3219   ctrl->server_local = xcalloc (1, sizeof *ctrl->server_local);
3220   ctrl->server_local->assuan_ctx = ctx;
3221   ctrl->server_local->use_cache_for_signing = 1;
3222   ctrl->digest.raw_value = 0;
3223
3224   assuan_set_io_monitor (ctx, io_monitor, NULL);
3225   agent_set_progress_cb (progress_cb, ctrl);
3226
3227   for (;;)
3228     {
3229       rc = assuan_accept (ctx);
3230       if (gpg_err_code (rc) == GPG_ERR_EOF || rc == -1)
3231         {
3232           break;
3233         }
3234       else if (rc)
3235         {
3236           log_info ("Assuan accept problem: %s\n", gpg_strerror (rc));
3237           break;
3238         }
3239
3240       rc = assuan_process (ctx);
3241       if (rc)
3242         {
3243           log_info ("Assuan processing failed: %s\n", gpg_strerror (rc));
3244           continue;
3245         }
3246     }
3247
3248   /* Reset the nonce caches.  */
3249   clear_nonce_cache (ctrl);
3250
3251   /* Reset the SCD if needed. */
3252   agent_reset_scd (ctrl);
3253
3254   /* Reset the pinentry (in case of popup messages). */
3255   agent_reset_query (ctrl);
3256
3257   /* Cleanup.  */
3258   assuan_release (ctx);
3259   xfree (ctrl->server_local->keydesc);
3260   xfree (ctrl->server_local->import_key);
3261   xfree (ctrl->server_local->export_key);
3262   if (ctrl->server_local->stopme)
3263     agent_exit (0);
3264   xfree (ctrl->server_local);
3265   ctrl->server_local = NULL;
3266 }
3267
3268
3269 /* Helper for the pinentry loopback mode.  It merely passes the
3270    parameters on to the client.  */
3271 gpg_error_t
3272 pinentry_loopback(ctrl_t ctrl, const char *keyword,
3273                   unsigned char **buffer, size_t *size,
3274                   size_t max_length)
3275 {
3276   gpg_error_t rc;
3277   assuan_context_t ctx = ctrl->server_local->assuan_ctx;
3278
3279   rc = print_assuan_status (ctx, "INQUIRE_MAXLEN", "%zu", max_length);
3280   if (rc)
3281     return rc;
3282
3283   assuan_begin_confidential (ctx);
3284   rc = assuan_inquire (ctx, keyword, buffer, size, max_length);
3285   assuan_end_confidential (ctx);
3286   return rc;
3287 }