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