chiark / gitweb /
dimrngr: Avoid need for hkp housekeeping.
[gnupg2.git] / agent / findkey.c
1 /* findkey.c - Locate the secret key
2  * Copyright (C) 2001, 2002, 2003, 2004, 2005, 2007,
3  *               2010, 2011 Free Software Foundation, Inc.
4  * Copyright (C) 2014 Werner Koch
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 <https://www.gnu.org/licenses/>.
20  */
21
22 #include <config.h>
23 #include <errno.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <ctype.h>
28 #include <fcntl.h>
29 #include <assert.h>
30 #include <unistd.h>
31 #include <sys/stat.h>
32 #include <assert.h>
33 #include <npth.h> /* (we use pth_sleep) */
34
35 #include "agent.h"
36 #include "i18n.h"
37 #include "../common/ssh-utils.h"
38 #include "../common/name-value.h"
39
40 #ifndef O_BINARY
41 #define O_BINARY 0
42 #endif
43
44 /* Helper to pass data to the check callback of the unprotect function. */
45 struct try_unprotect_arg_s
46 {
47   ctrl_t ctrl;
48   const unsigned char *protected_key;
49   unsigned char *unprotected_key;
50   int change_required; /* Set by the callback to indicate that the
51                           user should change the passphrase.  */
52 };
53
54
55 static gpg_error_t
56 write_extended_private_key (char *fname, estream_t fp,
57                             const void *buf, size_t len)
58 {
59   gpg_error_t err;
60   nvc_t pk = NULL;
61   gcry_sexp_t key = NULL;
62   int remove = 0;
63   int line;
64
65   err = nvc_parse_private_key (&pk, &line, fp);
66   if (err)
67     {
68       log_error ("error parsing '%s' line %d: %s\n",
69                  fname, line, gpg_strerror (err));
70       goto leave;
71     }
72
73   err = gcry_sexp_sscan (&key, NULL, buf, len);
74   if (err)
75     goto leave;
76
77   err = nvc_set_private_key (pk, key);
78   if (err)
79     goto leave;
80
81   err = es_fseek (fp, 0, SEEK_SET);
82   if (err)
83     goto leave;
84
85   err = nvc_write (pk, fp);
86   if (err)
87     {
88       log_error ("error writing '%s': %s\n", fname, gpg_strerror (err));
89       remove = 1;
90       goto leave;
91     }
92
93   if (ftruncate (es_fileno (fp), es_ftello (fp)))
94     {
95       err = gpg_error_from_syserror ();
96       log_error ("error truncating '%s': %s\n", fname, gpg_strerror (err));
97       remove = 1;
98       goto leave;
99     }
100
101   if (es_fclose (fp))
102     {
103       err = gpg_error_from_syserror ();
104       log_error ("error closing '%s': %s\n", fname, gpg_strerror (err));
105       remove = 1;
106       goto leave;
107     }
108   else
109     fp = NULL;
110
111   bump_key_eventcounter ();
112
113  leave:
114   if (fp)
115     es_fclose (fp);
116   if (remove)
117     gnupg_remove (fname);
118   xfree (fname);
119   gcry_sexp_release (key);
120   nvc_release (pk);
121   return err;
122 }
123
124 /* Write an S-expression formatted key to our key storage.  With FORCE
125    passed as true an existing key with the given GRIP will get
126    overwritten.  */
127 int
128 agent_write_private_key (const unsigned char *grip,
129                          const void *buffer, size_t length, int force)
130 {
131   char *fname;
132   estream_t fp;
133   char hexgrip[40+4+1];
134
135   bin2hex (grip, 20, hexgrip);
136   strcpy (hexgrip+40, ".key");
137
138   fname = make_filename (gnupg_homedir (), GNUPG_PRIVATE_KEYS_DIR,
139                          hexgrip, NULL);
140
141   /* FIXME: Write to a temp file first so that write failures during
142      key updates won't lead to a key loss.  */
143
144   if (!force && !access (fname, F_OK))
145     {
146       log_error ("secret key file '%s' already exists\n", fname);
147       xfree (fname);
148       return gpg_error (GPG_ERR_EEXIST);
149     }
150
151   fp = es_fopen (fname, force? "rb+,mode=-rw" : "wbx,mode=-rw");
152   if (!fp)
153     {
154       gpg_error_t tmperr = gpg_error_from_syserror ();
155
156       if (force && gpg_err_code (tmperr) == GPG_ERR_ENOENT)
157         {
158           fp = es_fopen (fname, "wbx,mode=-rw");
159           if (!fp)
160             tmperr = gpg_error_from_syserror ();
161         }
162       if (!fp)
163         {
164           log_error ("can't create '%s': %s\n", fname, gpg_strerror (tmperr));
165           xfree (fname);
166           return tmperr;
167         }
168     }
169   else if (force)
170     {
171       gpg_error_t rc;
172       char first;
173
174       /* See if an existing key is in extended format.  */
175       if (es_fread (&first, 1, 1, fp) != 1)
176         {
177           rc = gpg_error_from_syserror ();
178           log_error ("error reading first byte from '%s': %s\n",
179                      fname, strerror (errno));
180           xfree (fname);
181           es_fclose (fp);
182           return rc;
183         }
184
185       rc = es_fseek (fp, 0, SEEK_SET);
186       if (rc)
187         {
188           log_error ("error seeking in '%s': %s\n", fname, strerror (errno));
189           xfree (fname);
190           es_fclose (fp);
191           return rc;
192         }
193
194       if (first != '(')
195         {
196           /* Key is in extended format.  */
197           return write_extended_private_key (fname, fp, buffer, length);
198         }
199     }
200
201   if (es_fwrite (buffer, length, 1, fp) != 1)
202     {
203       gpg_error_t tmperr = gpg_error_from_syserror ();
204       log_error ("error writing '%s': %s\n", fname, gpg_strerror (tmperr));
205       es_fclose (fp);
206       gnupg_remove (fname);
207       xfree (fname);
208       return tmperr;
209     }
210
211   /* When force is given, the file might have to be truncated.  */
212   if (force && ftruncate (es_fileno (fp), es_ftello (fp)))
213     {
214       gpg_error_t tmperr = gpg_error_from_syserror ();
215       log_error ("error truncating '%s': %s\n", fname, gpg_strerror (tmperr));
216       es_fclose (fp);
217       gnupg_remove (fname);
218       xfree (fname);
219       return tmperr;
220     }
221
222   if (es_fclose (fp))
223     {
224       gpg_error_t tmperr = gpg_error_from_syserror ();
225       log_error ("error closing '%s': %s\n", fname, gpg_strerror (tmperr));
226       gnupg_remove (fname);
227       xfree (fname);
228       return tmperr;
229     }
230   bump_key_eventcounter ();
231   xfree (fname);
232   return 0;
233 }
234
235
236 /* Callback function to try the unprotection from the passphrase query
237    code. */
238 static gpg_error_t
239 try_unprotect_cb (struct pin_entry_info_s *pi)
240 {
241   struct try_unprotect_arg_s *arg = pi->check_cb_arg;
242   ctrl_t ctrl = arg->ctrl;
243   size_t dummy;
244   gpg_error_t err;
245   gnupg_isotime_t now, protected_at, tmptime;
246   char *desc = NULL;
247
248   assert (!arg->unprotected_key);
249
250   arg->change_required = 0;
251   err = agent_unprotect (ctrl, arg->protected_key, pi->pin, protected_at,
252                          &arg->unprotected_key, &dummy);
253   if (err)
254     return err;
255   if (!opt.max_passphrase_days || ctrl->in_passwd)
256     return 0;  /* No regular passphrase change required.  */
257
258   if (!*protected_at)
259     {
260       /* No protection date known - must force passphrase change.  */
261       desc = xtrystrdup (L_("Note: This passphrase has never been changed.%0A"
262                             "Please change it now."));
263       if (!desc)
264         return gpg_error_from_syserror ();
265     }
266   else
267     {
268       gnupg_get_isotime (now);
269       gnupg_copy_time (tmptime, protected_at);
270       err = add_days_to_isotime (tmptime, opt.max_passphrase_days);
271       if (err)
272         return err;
273       if (strcmp (now, tmptime) > 0 )
274         {
275           /* Passphrase "expired".  */
276           desc = xtryasprintf
277             (L_("This passphrase has not been changed%%0A"
278                 "since %.4s-%.2s-%.2s.  Please change it now."),
279              protected_at, protected_at+4, protected_at+6);
280           if (!desc)
281             return gpg_error_from_syserror ();
282         }
283     }
284
285   if (desc)
286     {
287       /* Change required.  */
288       if (opt.enforce_passphrase_constraints)
289         {
290           err = agent_get_confirmation (ctrl, desc,
291                                         L_("Change passphrase"), NULL, 0);
292           if (!err)
293             arg->change_required = 1;
294         }
295       else
296         {
297           err = agent_get_confirmation (ctrl, desc,
298                                         L_("Change passphrase"),
299                                         L_("I'll change it later"), 0);
300           if (!err)
301             arg->change_required = 1;
302           else if (gpg_err_code (err) == GPG_ERR_CANCELED
303                    || gpg_err_code (err) == GPG_ERR_FULLY_CANCELED)
304             err = 0;
305         }
306       xfree (desc);
307     }
308
309   return 0;
310 }
311
312
313 /* Modify a Key description, replacing certain special format
314    characters.  List of currently supported replacements:
315
316    %% - Replaced by a single %
317    %c - Replaced by the content of COMMENT.
318    %C - Same as %c but put into parentheses.
319    %F - Replaced by an ssh style fingerprint computed from KEY.
320
321    The functions returns 0 on success or an error code.  On success a
322    newly allocated string is stored at the address of RESULT.
323  */
324 static gpg_error_t
325 modify_description (const char *in, const char *comment, const gcry_sexp_t key,
326                     char **result)
327 {
328   size_t comment_length;
329   size_t in_len;
330   size_t out_len;
331   char *out;
332   size_t i;
333   int special, pass;
334   char *ssh_fpr = NULL;
335
336   comment_length = strlen (comment);
337   in_len  = strlen (in);
338
339   /* First pass calculates the length, second pass does the actual
340      copying.  */
341   out = NULL;
342   out_len = 0;
343   for (pass=0; pass < 2; pass++)
344     {
345       special = 0;
346       for (i = 0; i < in_len; i++)
347         {
348           if (special)
349             {
350               special = 0;
351               switch (in[i])
352                 {
353                 case '%':
354                   if (out)
355                     *out++ = '%';
356                   else
357                     out_len++;
358                   break;
359
360                 case 'c': /* Comment.  */
361                   if (out)
362                     {
363                       memcpy (out, comment, comment_length);
364                       out += comment_length;
365                     }
366                   else
367                     out_len += comment_length;
368                   break;
369
370                 case 'C': /* Comment.  */
371                   if (!comment_length)
372                     ;
373                   else if (out)
374                     {
375                       *out++ = '(';
376                       memcpy (out, comment, comment_length);
377                       out += comment_length;
378                       *out++ = ')';
379                     }
380                   else
381                     out_len += comment_length + 2;
382                   break;
383
384                 case 'F': /* SSH style fingerprint.  */
385                   if (!ssh_fpr && key)
386                     ssh_get_fingerprint_string (key, &ssh_fpr);
387                   if (ssh_fpr)
388                     {
389                       if (out)
390                         out = stpcpy (out, ssh_fpr);
391                       else
392                         out_len += strlen (ssh_fpr);
393                     }
394                   break;
395
396                 default: /* Invalid special sequences are kept as they are. */
397                   if (out)
398                     {
399                       *out++ = '%';
400                       *out++ = in[i];
401                     }
402                   else
403                     out_len+=2;
404                   break;
405                 }
406             }
407           else if (in[i] == '%')
408             special = 1;
409           else
410             {
411               if (out)
412                 *out++ = in[i];
413               else
414                 out_len++;
415             }
416         }
417
418       if (!pass)
419         {
420           *result = out = xtrymalloc (out_len + 1);
421           if (!out)
422             {
423               xfree (ssh_fpr);
424               return gpg_error_from_syserror ();
425             }
426         }
427     }
428
429   *out = 0;
430   assert (*result + out_len == out);
431   xfree (ssh_fpr);
432   return 0;
433 }
434
435
436
437 /* Unprotect the canconical encoded S-expression key in KEYBUF.  GRIP
438    should be the hex encoded keygrip of that key to be used with the
439    caching mechanism. DESC_TEXT may be set to override the default
440    description used for the pinentry.  If LOOKUP_TTL is given this
441    function is used to lookup the default ttl.  If R_PASSPHRASE is not
442    NULL, the function succeeded and the key was protected the used
443    passphrase (entered or from the cache) is stored there; if not NULL
444    will be stored.  The caller needs to free the returned
445    passphrase. */
446 static int
447 unprotect (ctrl_t ctrl, const char *cache_nonce, const char *desc_text,
448            unsigned char **keybuf, const unsigned char *grip,
449            cache_mode_t cache_mode, lookup_ttl_t lookup_ttl,
450            char **r_passphrase)
451 {
452   struct pin_entry_info_s *pi;
453   struct try_unprotect_arg_s arg;
454   int rc;
455   unsigned char *result;
456   size_t resultlen;
457   char hexgrip[40+1];
458
459   if (r_passphrase)
460     *r_passphrase = NULL;
461
462   bin2hex (grip, 20, hexgrip);
463
464   /* Initially try to get it using a cache nonce.  */
465   if (cache_nonce)
466     {
467       char *pw;
468
469       pw = agent_get_cache (cache_nonce, CACHE_MODE_NONCE);
470       if (pw)
471         {
472           rc = agent_unprotect (ctrl, *keybuf, pw, NULL, &result, &resultlen);
473           if (!rc)
474             {
475               if (r_passphrase)
476                 *r_passphrase = pw;
477               else
478                 xfree (pw);
479               xfree (*keybuf);
480               *keybuf = result;
481               return 0;
482             }
483           xfree (pw);
484         }
485     }
486
487   /* First try to get it from the cache - if there is none or we can't
488      unprotect it, we fall back to ask the user */
489   if (cache_mode != CACHE_MODE_IGNORE)
490     {
491       char *pw;
492
493     retry:
494       pw = agent_get_cache (hexgrip, cache_mode);
495       if (pw)
496         {
497           rc = agent_unprotect (ctrl, *keybuf, pw, NULL, &result, &resultlen);
498           if (!rc)
499             {
500               if (cache_mode == CACHE_MODE_NORMAL)
501                 agent_store_cache_hit (hexgrip);
502               if (r_passphrase)
503                 *r_passphrase = pw;
504               else
505                 xfree (pw);
506               xfree (*keybuf);
507               *keybuf = result;
508               return 0;
509             }
510           xfree (pw);
511           rc  = 0;
512         }
513       else if (cache_mode == CACHE_MODE_NORMAL)
514         {
515           /* The standard use of GPG keys is to have a signing and an
516              encryption subkey.  Commonly both use the same
517              passphrase.  We try to help the user to enter the
518              passphrase only once by silently trying the last
519              correctly entered passphrase.  Checking one additional
520              passphrase should be acceptable; despite the S2K
521              introduced delays. The assumed workflow is:
522
523                1. Read encrypted message in a MUA and thus enter a
524                   passphrase for the encryption subkey.
525
526                2. Reply to that mail with an encrypted and signed
527                   mail, thus entering the passphrase for the signing
528                   subkey.
529
530              We can often avoid the passphrase entry in the second
531              step.  We do this only in normal mode, so not to
532              interfere with unrelated cache entries.  */
533           pw = agent_get_cache (NULL, cache_mode);
534           if (pw)
535             {
536               rc = agent_unprotect (ctrl, *keybuf, pw, NULL,
537                                     &result, &resultlen);
538               if (!rc)
539                 {
540                   if (r_passphrase)
541                     *r_passphrase = pw;
542                   else
543                     xfree (pw);
544                   xfree (*keybuf);
545                   *keybuf = result;
546                   return 0;
547                 }
548               xfree (pw);
549               rc  = 0;
550             }
551         }
552
553       /* If the pinentry is currently in use, we wait up to 60 seconds
554          for it to close and check the cache again.  This solves a common
555          situation where several requests for unprotecting a key have
556          been made but the user is still entering the passphrase for
557          the first request.  Because all requests to agent_askpin are
558          serialized they would then pop up one after the other to
559          request the passphrase - despite that the user has already
560          entered it and is then available in the cache.  This
561          implementation is not race free but in the worst case the
562          user has to enter the passphrase only once more. */
563       if (pinentry_active_p (ctrl, 0))
564         {
565           /* Active - wait */
566           if (!pinentry_active_p (ctrl, 60))
567             {
568               /* We need to give the other thread a chance to actually put
569                  it into the cache. */
570               npth_sleep (1);
571               goto retry;
572             }
573           /* Timeout - better call pinentry now the plain way. */
574         }
575     }
576
577   pi = gcry_calloc_secure (1, sizeof (*pi) + MAX_PASSPHRASE_LEN + 1);
578   if (!pi)
579     return gpg_error_from_syserror ();
580   pi->max_length = MAX_PASSPHRASE_LEN + 1;
581   pi->min_digits = 0;  /* we want a real passphrase */
582   pi->max_digits = 16;
583   pi->max_tries = 3;
584   pi->check_cb = try_unprotect_cb;
585   arg.ctrl = ctrl;
586   arg.protected_key = *keybuf;
587   arg.unprotected_key = NULL;
588   arg.change_required = 0;
589   pi->check_cb_arg = &arg;
590
591   rc = agent_askpin (ctrl, desc_text, NULL, NULL, pi, hexgrip, cache_mode);
592   if (!rc)
593     {
594       assert (arg.unprotected_key);
595       if (arg.change_required)
596         {
597           /* The callback told as that the user should change their
598              passphrase.  Present the dialog to do.  */
599           size_t canlen, erroff;
600           gcry_sexp_t s_skey;
601
602           assert (arg.unprotected_key);
603           canlen = gcry_sexp_canon_len (arg.unprotected_key, 0, NULL, NULL);
604           rc = gcry_sexp_sscan (&s_skey, &erroff,
605                                 (char*)arg.unprotected_key, canlen);
606           if (rc)
607             {
608               log_error ("failed to build S-Exp (off=%u): %s\n",
609                          (unsigned int)erroff, gpg_strerror (rc));
610               wipememory (arg.unprotected_key, canlen);
611               xfree (arg.unprotected_key);
612               xfree (pi);
613               return rc;
614             }
615           rc = agent_protect_and_store (ctrl, s_skey, NULL);
616           gcry_sexp_release (s_skey);
617           if (rc)
618             {
619               log_error ("changing the passphrase failed: %s\n",
620                          gpg_strerror (rc));
621               wipememory (arg.unprotected_key, canlen);
622               xfree (arg.unprotected_key);
623               xfree (pi);
624               return rc;
625             }
626         }
627       else
628         {
629           /* Passphrase is fine.  */
630           agent_put_cache (hexgrip, cache_mode, pi->pin,
631                            lookup_ttl? lookup_ttl (hexgrip) : 0);
632           agent_store_cache_hit (hexgrip);
633           if (r_passphrase && *pi->pin)
634             *r_passphrase = xtrystrdup (pi->pin);
635         }
636       xfree (*keybuf);
637       *keybuf = arg.unprotected_key;
638     }
639   xfree (pi);
640   return rc;
641 }
642
643
644 /* Read the key identified by GRIP from the private key directory and
645    return it as an gcrypt S-expression object in RESULT.  On failure
646    returns an error code and stores NULL at RESULT. */
647 static gpg_error_t
648 read_key_file (const unsigned char *grip, gcry_sexp_t *result)
649 {
650   int rc;
651   char *fname;
652   estream_t fp;
653   struct stat st;
654   unsigned char *buf;
655   size_t buflen, erroff;
656   gcry_sexp_t s_skey;
657   char hexgrip[40+4+1];
658   char first;
659
660   *result = NULL;
661
662   bin2hex (grip, 20, hexgrip);
663   strcpy (hexgrip+40, ".key");
664
665   fname = make_filename (gnupg_homedir (), GNUPG_PRIVATE_KEYS_DIR,
666                          hexgrip, NULL);
667   fp = es_fopen (fname, "rb");
668   if (!fp)
669     {
670       rc = gpg_error_from_syserror ();
671       if (gpg_err_code (rc) != GPG_ERR_ENOENT)
672         log_error ("can't open '%s': %s\n", fname, strerror (errno));
673       xfree (fname);
674       return rc;
675     }
676
677   if (es_fread (&first, 1, 1, fp) != 1)
678     {
679       rc = gpg_error_from_syserror ();
680       log_error ("error reading first byte from '%s': %s\n",
681                  fname, strerror (errno));
682       xfree (fname);
683       es_fclose (fp);
684       return rc;
685     }
686
687   rc = es_fseek (fp, 0, SEEK_SET);
688   if (rc)
689     {
690       log_error ("error seeking in '%s': %s\n", fname, strerror (errno));
691       xfree (fname);
692       es_fclose (fp);
693       return rc;
694     }
695
696   if (first != '(')
697     {
698       /* Key is in extended format.  */
699       nvc_t pk;
700       int line;
701
702       rc = nvc_parse_private_key (&pk, &line, fp);
703       es_fclose (fp);
704
705       if (rc)
706         log_error ("error parsing '%s' line %d: %s\n",
707                    fname, line, gpg_strerror (rc));
708       else
709         {
710           rc = nvc_get_private_key (pk, result);
711           nvc_release (pk);
712           if (rc)
713             log_error ("error getting private key from '%s': %s\n",
714                        fname, gpg_strerror (rc));
715         }
716
717       xfree (fname);
718       return rc;
719     }
720
721   if (fstat (es_fileno (fp), &st))
722     {
723       rc = gpg_error_from_syserror ();
724       log_error ("can't stat '%s': %s\n", fname, strerror (errno));
725       xfree (fname);
726       es_fclose (fp);
727       return rc;
728     }
729
730   buflen = st.st_size;
731   buf = xtrymalloc (buflen+1);
732   if (!buf)
733     {
734       rc = gpg_error_from_syserror ();
735       log_error ("error allocating %zu bytes for '%s': %s\n",
736                  buflen, fname, strerror (errno));
737       xfree (fname);
738       es_fclose (fp);
739       xfree (buf);
740       return rc;
741
742     }
743
744   if (es_fread (buf, buflen, 1, fp) != 1)
745     {
746       rc = gpg_error_from_syserror ();
747       log_error ("error reading %zu bytes from '%s': %s\n",
748                  buflen, fname, strerror (errno));
749       xfree (fname);
750       es_fclose (fp);
751       xfree (buf);
752       return rc;
753     }
754
755   /* Convert the file into a gcrypt S-expression object.  */
756   rc = gcry_sexp_sscan (&s_skey, &erroff, (char*)buf, buflen);
757   xfree (fname);
758   es_fclose (fp);
759   xfree (buf);
760   if (rc)
761     {
762       log_error ("failed to build S-Exp (off=%u): %s\n",
763                  (unsigned int)erroff, gpg_strerror (rc));
764       return rc;
765     }
766   *result = s_skey;
767   return 0;
768 }
769
770
771 /* Remove the key identified by GRIP from the private key directory.  */
772 static gpg_error_t
773 remove_key_file (const unsigned char *grip)
774 {
775   gpg_error_t err = 0;
776   char *fname;
777   char hexgrip[40+4+1];
778
779   bin2hex (grip, 20, hexgrip);
780   strcpy (hexgrip+40, ".key");
781   fname = make_filename (gnupg_homedir (), GNUPG_PRIVATE_KEYS_DIR,
782                          hexgrip, NULL);
783   if (gnupg_remove (fname))
784     err = gpg_error_from_syserror ();
785   xfree (fname);
786   return err;
787 }
788
789
790 /* Return the secret key as an S-Exp in RESULT after locating it using
791    the GRIP.  If the operation shall be diverted to a token, an
792    allocated S-expression with the shadow_info part from the file is
793    stored at SHADOW_INFO; if not NULL will be stored at SHADOW_INFO.
794    CACHE_MODE defines now the cache shall be used.  DESC_TEXT may be
795    set to present a custom description for the pinentry.  LOOKUP_TTL
796    is an optional function to convey a TTL to the cache manager; we do
797    not simply pass the TTL value because the value is only needed if
798    an unprotect action was needed and looking up the TTL may have some
799    overhead (e.g. scanning the sshcontrol file).  If a CACHE_NONCE is
800    given that cache item is first tried to get a passphrase.  If
801    R_PASSPHRASE is not NULL, the function succeeded and the key was
802    protected the used passphrase (entered or from the cache) is stored
803    there; if not NULL will be stored.  The caller needs to free the
804    returned passphrase.   */
805 gpg_error_t
806 agent_key_from_file (ctrl_t ctrl, const char *cache_nonce,
807                      const char *desc_text,
808                      const unsigned char *grip, unsigned char **shadow_info,
809                      cache_mode_t cache_mode, lookup_ttl_t lookup_ttl,
810                      gcry_sexp_t *result, char **r_passphrase)
811 {
812   int rc;
813   unsigned char *buf;
814   size_t len, buflen, erroff;
815   gcry_sexp_t s_skey;
816
817   *result = NULL;
818   if (shadow_info)
819     *shadow_info = NULL;
820   if (r_passphrase)
821     *r_passphrase = NULL;
822
823   rc = read_key_file (grip, &s_skey);
824   if (rc)
825     {
826       if (gpg_err_code (rc) == GPG_ERR_ENOENT)
827         rc = gpg_error (GPG_ERR_NO_SECKEY);
828       return rc;
829     }
830
831   /* For use with the protection functions we also need the key as an
832      canonical encoded S-expression in a buffer.  Create this buffer
833      now.  */
834   rc = make_canon_sexp (s_skey, &buf, &len);
835   if (rc)
836     return rc;
837
838   switch (agent_private_key_type (buf))
839     {
840     case PRIVATE_KEY_CLEAR:
841       break; /* no unprotection needed */
842     case PRIVATE_KEY_OPENPGP_NONE:
843       {
844         unsigned char *buf_new;
845         size_t buf_newlen;
846
847         rc = agent_unprotect (ctrl, buf, "", NULL, &buf_new, &buf_newlen);
848         if (rc)
849           log_error ("failed to convert unprotected openpgp key: %s\n",
850                      gpg_strerror (rc));
851         else
852           {
853             xfree (buf);
854             buf = buf_new;
855           }
856       }
857       break;
858     case PRIVATE_KEY_PROTECTED:
859       {
860         char *desc_text_final;
861         char *comment = NULL;
862
863         /* Note, that we will take the comment as a C string for
864            display purposes; i.e. all stuff beyond a Nul character is
865            ignored.  */
866         {
867           gcry_sexp_t comment_sexp;
868
869           comment_sexp = gcry_sexp_find_token (s_skey, "comment", 0);
870           if (comment_sexp)
871             comment = gcry_sexp_nth_string (comment_sexp, 1);
872           gcry_sexp_release (comment_sexp);
873         }
874
875         desc_text_final = NULL;
876         if (desc_text)
877           rc = modify_description (desc_text, comment? comment:"", s_skey,
878                                    &desc_text_final);
879         gcry_free (comment);
880
881         if (!rc)
882           {
883             rc = unprotect (ctrl, cache_nonce, desc_text_final, &buf, grip,
884                             cache_mode, lookup_ttl, r_passphrase);
885             if (rc)
886               log_error ("failed to unprotect the secret key: %s\n",
887                          gpg_strerror (rc));
888           }
889
890         xfree (desc_text_final);
891       }
892       break;
893     case PRIVATE_KEY_SHADOWED:
894       if (shadow_info)
895         {
896           const unsigned char *s;
897           size_t n;
898
899           rc = agent_get_shadow_info (buf, &s);
900           if (!rc)
901             {
902               n = gcry_sexp_canon_len (s, 0, NULL,NULL);
903               assert (n);
904               *shadow_info = xtrymalloc (n);
905               if (!*shadow_info)
906                 rc = out_of_core ();
907               else
908                 {
909                   memcpy (*shadow_info, s, n);
910                   rc = 0;
911                 }
912             }
913           if (rc)
914             log_error ("get_shadow_info failed: %s\n", gpg_strerror (rc));
915         }
916       else
917         rc = gpg_error (GPG_ERR_UNUSABLE_SECKEY);
918       break;
919     default:
920       log_error ("invalid private key format\n");
921       rc = gpg_error (GPG_ERR_BAD_SECKEY);
922       break;
923     }
924   gcry_sexp_release (s_skey);
925   s_skey = NULL;
926   if (rc)
927     {
928       xfree (buf);
929       if (r_passphrase)
930         {
931           xfree (*r_passphrase);
932           *r_passphrase = NULL;
933         }
934       return rc;
935     }
936
937   buflen = gcry_sexp_canon_len (buf, 0, NULL, NULL);
938   rc = gcry_sexp_sscan (&s_skey, &erroff, (char*)buf, buflen);
939   wipememory (buf, buflen);
940   xfree (buf);
941   if (rc)
942     {
943       log_error ("failed to build S-Exp (off=%u): %s\n",
944                  (unsigned int)erroff, gpg_strerror (rc));
945       if (r_passphrase)
946         {
947           xfree (*r_passphrase);
948           *r_passphrase = NULL;
949         }
950       return rc;
951     }
952
953   *result = s_skey;
954   return 0;
955 }
956
957
958 /* Return the string name from the S-expression S_KEY as well as a
959    string describing the names of the parameters.  ALGONAMESIZE and
960    ELEMSSIZE give the allocated size of the provided buffers.  The
961    buffers may be NULL if not required.  If R_LIST is not NULL the top
962    level list will be stored there; the caller needs to release it in
963    this case.  */
964 static gpg_error_t
965 key_parms_from_sexp (gcry_sexp_t s_key, gcry_sexp_t *r_list,
966                      char *r_algoname, size_t algonamesize,
967                      char *r_elems, size_t elemssize)
968 {
969   gcry_sexp_t list, l2;
970   const char *name, *algoname, *elems;
971   size_t n;
972
973   if (r_list)
974     *r_list = NULL;
975
976   list = gcry_sexp_find_token (s_key, "shadowed-private-key", 0 );
977   if (!list)
978     list = gcry_sexp_find_token (s_key, "protected-private-key", 0 );
979   if (!list)
980     list = gcry_sexp_find_token (s_key, "private-key", 0 );
981   if (!list)
982     {
983       log_error ("invalid private key format\n");
984       return gpg_error (GPG_ERR_BAD_SECKEY);
985     }
986
987   l2 = gcry_sexp_cadr (list);
988   gcry_sexp_release (list);
989   list = l2;
990   name = gcry_sexp_nth_data (list, 0, &n);
991   if (n==3 && !memcmp (name, "rsa", 3))
992     {
993       algoname = "rsa";
994       elems = "ne";
995     }
996   else if (n==3 && !memcmp (name, "dsa", 3))
997     {
998       algoname = "dsa";
999       elems = "pqgy";
1000     }
1001   else if (n==3 && !memcmp (name, "ecc", 3))
1002     {
1003       algoname = "ecc";
1004       elems = "pabgnq";
1005     }
1006   else if (n==5 && !memcmp (name, "ecdsa", 5))
1007     {
1008       algoname = "ecdsa";
1009       elems = "pabgnq";
1010     }
1011   else if (n==4 && !memcmp (name, "ecdh", 4))
1012     {
1013       algoname = "ecdh";
1014       elems = "pabgnq";
1015     }
1016   else if (n==3 && !memcmp (name, "elg", 3))
1017     {
1018       algoname = "elg";
1019       elems = "pgy";
1020     }
1021   else
1022     {
1023       log_error ("unknown private key algorithm\n");
1024       gcry_sexp_release (list);
1025       return gpg_error (GPG_ERR_BAD_SECKEY);
1026     }
1027
1028   if (r_algoname)
1029     {
1030       if (strlen (algoname) >= algonamesize)
1031         return gpg_error (GPG_ERR_BUFFER_TOO_SHORT);
1032       strcpy (r_algoname, algoname);
1033     }
1034   if (r_elems)
1035     {
1036       if (strlen (elems) >= elemssize)
1037         return gpg_error (GPG_ERR_BUFFER_TOO_SHORT);
1038       strcpy (r_elems, elems);
1039     }
1040
1041   if (r_list)
1042     *r_list = list;
1043   else
1044     gcry_sexp_release (list);
1045
1046   return 0;
1047 }
1048
1049
1050 /* Return true if KEYPARMS holds an EdDSA key.  */
1051 static int
1052 is_eddsa (gcry_sexp_t keyparms)
1053 {
1054   int result = 0;
1055   gcry_sexp_t list;
1056   const char *s;
1057   size_t n;
1058   int i;
1059
1060   list = gcry_sexp_find_token (keyparms, "flags", 0);
1061   for (i = list ? gcry_sexp_length (list)-1 : 0; i > 0; i--)
1062     {
1063       s = gcry_sexp_nth_data (list, i, &n);
1064       if (!s)
1065         continue; /* Not a data element. */
1066
1067       if (n == 5 && !memcmp (s, "eddsa", 5))
1068         {
1069           result = 1;
1070           break;
1071         }
1072     }
1073   gcry_sexp_release (list);
1074   return result;
1075 }
1076
1077
1078 /* Return the public key algorithm number if S_KEY is a DSA style key.
1079    If it is not a DSA style key, return 0.  */
1080 int
1081 agent_is_dsa_key (gcry_sexp_t s_key)
1082 {
1083   int result;
1084   gcry_sexp_t list;
1085   char algoname[6];
1086
1087   if (!s_key)
1088     return 0;
1089
1090   if (key_parms_from_sexp (s_key, &list, algoname, sizeof algoname, NULL, 0))
1091     return 0; /* Error - assume it is not an DSA key.  */
1092
1093   if (!strcmp (algoname, "dsa"))
1094     result = GCRY_PK_DSA;
1095   else if (!strcmp (algoname, "ecc"))
1096     {
1097       if (is_eddsa (list))
1098         result = 0;
1099       else
1100         result = GCRY_PK_ECDSA;
1101     }
1102   else if (!strcmp (algoname, "ecdsa"))
1103     result = GCRY_PK_ECDSA;
1104   else
1105     result = 0;
1106
1107   gcry_sexp_release (list);
1108   return result;
1109 }
1110
1111
1112 /* Return true if S_KEY is an EdDSA key as used with curve Ed25519.  */
1113 int
1114 agent_is_eddsa_key (gcry_sexp_t s_key)
1115 {
1116   int result;
1117   gcry_sexp_t list;
1118   char algoname[6];
1119
1120   if (!s_key)
1121     return 0;
1122
1123   if (key_parms_from_sexp (s_key, &list, algoname, sizeof algoname, NULL, 0))
1124     return 0; /* Error - assume it is not an EdDSA key.  */
1125
1126   if (!strcmp (algoname, "ecc") && is_eddsa (list))
1127     result = 1;
1128   else if (!strcmp (algoname, "eddsa")) /* backward compatibility.  */
1129     result = 1;
1130   else
1131     result = 0;
1132
1133   gcry_sexp_release (list);
1134   return result;
1135 }
1136
1137
1138 /* Return the key for the keygrip GRIP.  The result is stored at
1139    RESULT.  This function extracts the key from the private key
1140    database and returns it as an S-expression object as it is.  On
1141    failure an error code is returned and NULL stored at RESULT. */
1142 gpg_error_t
1143 agent_raw_key_from_file (ctrl_t ctrl, const unsigned char *grip,
1144                          gcry_sexp_t *result)
1145 {
1146   gpg_error_t err;
1147   gcry_sexp_t s_skey;
1148
1149   (void)ctrl;
1150
1151   *result = NULL;
1152
1153   err = read_key_file (grip, &s_skey);
1154   if (!err)
1155     *result = s_skey;
1156   return err;
1157 }
1158
1159
1160 /* Return the public key for the keygrip GRIP.  The result is stored
1161    at RESULT.  This function extracts the public key from the private
1162    key database.  On failure an error code is returned and NULL stored
1163    at RESULT. */
1164 gpg_error_t
1165 agent_public_key_from_file (ctrl_t ctrl,
1166                             const unsigned char *grip,
1167                             gcry_sexp_t *result)
1168 {
1169   gpg_error_t err;
1170   int i, idx;
1171   gcry_sexp_t s_skey;
1172   const char *algoname, *elems;
1173   int npkey;
1174   gcry_mpi_t array[10];
1175   gcry_sexp_t curve = NULL;
1176   gcry_sexp_t flags = NULL;
1177   gcry_sexp_t uri_sexp, comment_sexp;
1178   const char *uri, *comment;
1179   size_t uri_length, comment_length;
1180   char *format, *p;
1181   void *args[2+7+2+2+1]; /* Size is 2 + max. # of elements + 2 for uri + 2
1182                             for comment + end-of-list.  */
1183   int argidx;
1184   gcry_sexp_t list = NULL;
1185   const char *s;
1186
1187   (void)ctrl;
1188
1189   *result = NULL;
1190
1191   err = read_key_file (grip, &s_skey);
1192   if (err)
1193     return err;
1194
1195   for (i=0; i < DIM (array); i++)
1196     array[i] = NULL;
1197
1198   err = extract_private_key (s_skey, 0, &algoname, &npkey, NULL, &elems,
1199                              array, DIM (array), &curve, &flags);
1200   if (err)
1201     {
1202       gcry_sexp_release (s_skey);
1203       return err;
1204     }
1205
1206   uri = NULL;
1207   uri_length = 0;
1208   uri_sexp = gcry_sexp_find_token (s_skey, "uri", 0);
1209   if (uri_sexp)
1210     uri = gcry_sexp_nth_data (uri_sexp, 1, &uri_length);
1211
1212   comment = NULL;
1213   comment_length = 0;
1214   comment_sexp = gcry_sexp_find_token (s_skey, "comment", 0);
1215   if (comment_sexp)
1216     comment = gcry_sexp_nth_data (comment_sexp, 1, &comment_length);
1217
1218   gcry_sexp_release (s_skey);
1219   s_skey = NULL;
1220
1221
1222   /* FIXME: The following thing is pretty ugly code; we should
1223      investigate how to make it cleaner.  Probably code to handle
1224      canonical S-expressions in a memory buffer is better suited for
1225      such a task.  After all that is what we do in protect.c.  Neeed
1226      to find common patterns and write a straightformward API to use
1227      them.  */
1228   assert (sizeof (size_t) <= sizeof (void*));
1229
1230   format = xtrymalloc (15+4+7*npkey+10+15+1+1);
1231   if (!format)
1232     {
1233       err = gpg_error_from_syserror ();
1234       for (i=0; array[i]; i++)
1235         gcry_mpi_release (array[i]);
1236       gcry_sexp_release (curve);
1237       gcry_sexp_release (flags);
1238       gcry_sexp_release (uri_sexp);
1239       gcry_sexp_release (comment_sexp);
1240       return err;
1241     }
1242
1243   argidx = 0;
1244   p = stpcpy (stpcpy (format, "(public-key("), algoname);
1245   p = stpcpy (p, "%S%S");       /* curve name and flags.  */
1246   args[argidx++] = &curve;
1247   args[argidx++] = &flags;
1248   for (idx=0, s=elems; idx < npkey; idx++)
1249     {
1250       *p++ = '(';
1251       *p++ = *s++;
1252       p = stpcpy (p, " %m)");
1253       assert (argidx < DIM (args));
1254       args[argidx++] = &array[idx];
1255     }
1256   *p++ = ')';
1257   if (uri)
1258     {
1259       p = stpcpy (p, "(uri %b)");
1260       assert (argidx+1 < DIM (args));
1261       args[argidx++] = (void *)&uri_length;
1262       args[argidx++] = (void *)&uri;
1263     }
1264   if (comment)
1265     {
1266       p = stpcpy (p, "(comment %b)");
1267       assert (argidx+1 < DIM (args));
1268       args[argidx++] = (void *)&comment_length;
1269       args[argidx++] = (void*)&comment;
1270     }
1271   *p++ = ')';
1272   *p = 0;
1273   assert (argidx < DIM (args));
1274   args[argidx] = NULL;
1275
1276   err = gcry_sexp_build_array (&list, NULL, format, args);
1277   xfree (format);
1278   for (i=0; array[i]; i++)
1279     gcry_mpi_release (array[i]);
1280   gcry_sexp_release (curve);
1281   gcry_sexp_release (flags);
1282   gcry_sexp_release (uri_sexp);
1283   gcry_sexp_release (comment_sexp);
1284
1285   if (!err)
1286     *result = list;
1287   return err;
1288 }
1289
1290
1291
1292 /* Check whether the the secret key identified by GRIP is available.
1293    Returns 0 is the key is available.  */
1294 int
1295 agent_key_available (const unsigned char *grip)
1296 {
1297   int result;
1298   char *fname;
1299   char hexgrip[40+4+1];
1300
1301   bin2hex (grip, 20, hexgrip);
1302   strcpy (hexgrip+40, ".key");
1303
1304   fname = make_filename (gnupg_homedir (), GNUPG_PRIVATE_KEYS_DIR,
1305                          hexgrip, NULL);
1306   result = !access (fname, R_OK)? 0 : -1;
1307   xfree (fname);
1308   return result;
1309 }
1310
1311
1312
1313 /* Return the information about the secret key specified by the binary
1314    keygrip GRIP.  If the key is a shadowed one the shadow information
1315    will be stored at the address R_SHADOW_INFO as an allocated
1316    S-expression.  */
1317 gpg_error_t
1318 agent_key_info_from_file (ctrl_t ctrl, const unsigned char *grip,
1319                           int *r_keytype, unsigned char **r_shadow_info)
1320 {
1321   gpg_error_t err;
1322   unsigned char *buf;
1323   size_t len;
1324   int keytype;
1325
1326   (void)ctrl;
1327
1328   if (r_keytype)
1329     *r_keytype = PRIVATE_KEY_UNKNOWN;
1330   if (r_shadow_info)
1331     *r_shadow_info = NULL;
1332
1333   {
1334     gcry_sexp_t sexp;
1335
1336     err = read_key_file (grip, &sexp);
1337     if (err)
1338       {
1339         if (gpg_err_code (err) == GPG_ERR_ENOENT)
1340           return gpg_error (GPG_ERR_NOT_FOUND);
1341         else
1342           return err;
1343       }
1344     err = make_canon_sexp (sexp, &buf, &len);
1345     gcry_sexp_release (sexp);
1346     if (err)
1347       return err;
1348   }
1349
1350   keytype = agent_private_key_type (buf);
1351   switch (keytype)
1352     {
1353     case PRIVATE_KEY_CLEAR:
1354     case PRIVATE_KEY_OPENPGP_NONE:
1355       break;
1356     case PRIVATE_KEY_PROTECTED:
1357       /* If we ever require it we could retrieve the comment fields
1358          from such a key. */
1359       break;
1360     case PRIVATE_KEY_SHADOWED:
1361       if (r_shadow_info)
1362         {
1363           const unsigned char *s;
1364           size_t n;
1365
1366           err = agent_get_shadow_info (buf, &s);
1367           if (!err)
1368             {
1369               n = gcry_sexp_canon_len (s, 0, NULL, NULL);
1370               assert (n);
1371               *r_shadow_info = xtrymalloc (n);
1372               if (!*r_shadow_info)
1373                 err = gpg_error_from_syserror ();
1374               else
1375                 memcpy (*r_shadow_info, s, n);
1376             }
1377         }
1378       break;
1379     default:
1380       err = gpg_error (GPG_ERR_BAD_SECKEY);
1381       break;
1382     }
1383
1384   if (!err && r_keytype)
1385     *r_keytype = keytype;
1386
1387   xfree (buf);
1388   return err;
1389 }
1390
1391
1392 \f
1393 /* Delete the key with GRIP from the disk after having asked for
1394    confirmation using DESC_TEXT.  If FORCE is set the function won't
1395    require a confirmation via Pinentry or warns if the key is also
1396    used by ssh.
1397
1398    Common error codes are:
1399      GPG_ERR_NO_SECKEY
1400      GPG_ERR_KEY_ON_CARD
1401      GPG_ERR_NOT_CONFIRMED
1402 */
1403 gpg_error_t
1404 agent_delete_key (ctrl_t ctrl, const char *desc_text,
1405                   const unsigned char *grip, int force)
1406 {
1407   gpg_error_t err;
1408   gcry_sexp_t s_skey = NULL;
1409   unsigned char *buf = NULL;
1410   size_t len;
1411   char *desc_text_final = NULL;
1412   char *comment = NULL;
1413   ssh_control_file_t cf = NULL;
1414   char hexgrip[40+4+1];
1415   char *default_desc = NULL;
1416
1417   err = read_key_file (grip, &s_skey);
1418   if (gpg_err_code (err) == GPG_ERR_ENOENT)
1419     err = gpg_error (GPG_ERR_NO_SECKEY);
1420   if (err)
1421     goto leave;
1422
1423   err = make_canon_sexp (s_skey, &buf, &len);
1424   if (err)
1425     goto leave;
1426
1427   switch (agent_private_key_type (buf))
1428     {
1429     case PRIVATE_KEY_CLEAR:
1430     case PRIVATE_KEY_OPENPGP_NONE:
1431     case PRIVATE_KEY_PROTECTED:
1432       bin2hex (grip, 20, hexgrip);
1433       if (!force)
1434         {
1435           if (!desc_text)
1436             {
1437               default_desc = xtryasprintf
1438           (L_("Do you really want to delete the key identified by keygrip%%0A"
1439               "  %s%%0A  %%C%%0A?"), hexgrip);
1440               desc_text = default_desc;
1441             }
1442
1443           /* Note, that we will take the comment as a C string for
1444              display purposes; i.e. all stuff beyond a Nul character is
1445              ignored.  */
1446           {
1447             gcry_sexp_t comment_sexp;
1448
1449             comment_sexp = gcry_sexp_find_token (s_skey, "comment", 0);
1450             if (comment_sexp)
1451               comment = gcry_sexp_nth_string (comment_sexp, 1);
1452             gcry_sexp_release (comment_sexp);
1453           }
1454
1455           if (desc_text)
1456             err = modify_description (desc_text, comment? comment:"", s_skey,
1457                                       &desc_text_final);
1458           if (err)
1459             goto leave;
1460
1461           err = agent_get_confirmation (ctrl, desc_text_final,
1462                                         L_("Delete key"), L_("No"), 0);
1463           if (err)
1464             goto leave;
1465
1466           cf = ssh_open_control_file ();
1467           if (cf)
1468             {
1469               if (!ssh_search_control_file (cf, hexgrip, NULL, NULL, NULL))
1470                 {
1471                   err = agent_get_confirmation
1472                     (ctrl,
1473                      L_("Warning: This key is also listed for use with SSH!\n"
1474                         "Deleting the key might remove your ability to "
1475                         "access remote machines."),
1476                      L_("Delete key"), L_("No"), 0);
1477                   if (err)
1478                     goto leave;
1479                 }
1480             }
1481         }
1482       err = remove_key_file (grip);
1483       break;
1484
1485     case PRIVATE_KEY_SHADOWED:
1486       err = remove_key_file (grip);
1487       break;
1488
1489     default:
1490       log_error ("invalid private key format\n");
1491       err = gpg_error (GPG_ERR_BAD_SECKEY);
1492       break;
1493     }
1494
1495  leave:
1496   ssh_close_control_file (cf);
1497   gcry_free (comment);
1498   xfree (desc_text_final);
1499   xfree (default_desc);
1500   xfree (buf);
1501   gcry_sexp_release (s_skey);
1502   return err;
1503 }
1504
1505
1506 /* Write an S-expression formatted shadow key to our key storage.
1507    Shadow key is created by an S-expression public key in PKBUF and
1508    card's SERIALNO and the IDSTRING.  With FORCE passed as true an
1509    existing key with the given GRIP will get overwritten.  */
1510 gpg_error_t
1511 agent_write_shadow_key (const unsigned char *grip,
1512                         const char *serialno, const char *keyid,
1513                         const unsigned char *pkbuf, int force)
1514 {
1515   gpg_error_t err;
1516   unsigned char *shadow_info;
1517   unsigned char *shdkey;
1518   size_t len;
1519
1520   shadow_info = make_shadow_info (serialno, keyid);
1521   if (!shadow_info)
1522     return gpg_error_from_syserror ();
1523
1524   err = agent_shadow_key (pkbuf, shadow_info, &shdkey);
1525   xfree (shadow_info);
1526   if (err)
1527     {
1528       log_error ("shadowing the key failed: %s\n", gpg_strerror (err));
1529       return err;
1530     }
1531
1532   len = gcry_sexp_canon_len (shdkey, 0, NULL, NULL);
1533   err = agent_write_private_key (grip, shdkey, len, force);
1534   xfree (shdkey);
1535   if (err)
1536     log_error ("error writing key: %s\n", gpg_strerror (err));
1537
1538   return err;
1539 }