Imported Upstream version 2.1.13
[platform/upstream/gpg2.git] / g10 / export.c
1 /* export.c - Export keys in the OpenPGP defined format.
2  * Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004,
3  *               2005, 2010 Free Software Foundation, Inc.
4  * Copyright (C) 1998-2016  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 <http://www.gnu.org/licenses/>.
20  */
21
22 #include <config.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <errno.h>
27
28 #include "gpg.h"
29 #include "options.h"
30 #include "packet.h"
31 #include "status.h"
32 #include "keydb.h"
33 #include "util.h"
34 #include "main.h"
35 #include "i18n.h"
36 #include "membuf.h"
37 #include "host2net.h"
38 #include "trustdb.h"
39 #include "call-agent.h"
40
41 /* An object to keep track of subkeys. */
42 struct subkey_list_s
43 {
44   struct subkey_list_s *next;
45   u32 kid[2];
46 };
47 typedef struct subkey_list_s *subkey_list_t;
48
49
50 /* An object to track statistics for export operations.  */
51 struct export_stats_s
52 {
53   ulong count;            /* Number of processed keys.        */
54   ulong secret_count;     /* Number of secret keys seen.      */
55   ulong exported;         /* Number of actual exported keys.  */
56 };
57
58
59 /* Local prototypes.  */
60 static int do_export (ctrl_t ctrl, strlist_t users, int secret,
61                       unsigned int options, export_stats_t stats);
62 static int do_export_stream (ctrl_t ctrl, iobuf_t out,
63                              strlist_t users, int secret,
64                              kbnode_t *keyblock_out, unsigned int options,
65                              export_stats_t stats, int *any);
66
67 \f
68
69
70 /* Option parser for export options.  See parse_options fro
71    details.  */
72 int
73 parse_export_options(char *str,unsigned int *options,int noisy)
74 {
75   struct parse_options export_opts[]=
76     {
77       {"export-local-sigs",EXPORT_LOCAL_SIGS,NULL,
78        N_("export signatures that are marked as local-only")},
79       {"export-attributes",EXPORT_ATTRIBUTES,NULL,
80        N_("export attribute user IDs (generally photo IDs)")},
81       {"export-sensitive-revkeys",EXPORT_SENSITIVE_REVKEYS,NULL,
82        N_("export revocation keys marked as \"sensitive\"")},
83       {"export-clean",EXPORT_CLEAN,NULL,
84        N_("remove unusable parts from key during export")},
85       {"export-minimal",EXPORT_MINIMAL|EXPORT_CLEAN,NULL,
86        N_("remove as much as possible from key during export")},
87       /* Aliases for backward compatibility */
88       {"include-local-sigs",EXPORT_LOCAL_SIGS,NULL,NULL},
89       {"include-attributes",EXPORT_ATTRIBUTES,NULL,NULL},
90       {"include-sensitive-revkeys",EXPORT_SENSITIVE_REVKEYS,NULL,NULL},
91       /* dummy */
92       {"export-unusable-sigs",0,NULL,NULL},
93       {"export-clean-sigs",0,NULL,NULL},
94       {"export-clean-uids",0,NULL,NULL},
95       {NULL,0,NULL,NULL}
96       /* add tags for include revoked and disabled? */
97     };
98
99   return parse_options(str,options,export_opts,noisy);
100 }
101
102
103 /* Create a new export stats object initialized to zero.  On error
104    returns NULL and sets ERRNO.  */
105 export_stats_t
106 export_new_stats (void)
107 {
108   export_stats_t stats;
109
110   return xtrycalloc (1, sizeof *stats);
111 }
112
113
114 /* Release an export stats object.  */
115 void
116 export_release_stats (export_stats_t stats)
117 {
118   xfree (stats);
119 }
120
121
122 /* Print export statistics using the status interface.  */
123 void
124 export_print_stats (export_stats_t stats)
125 {
126   if (!stats)
127     return;
128
129   if (is_status_enabled ())
130     {
131       char buf[15*20];
132
133       snprintf (buf, sizeof buf, "%lu %lu %lu",
134                 stats->count,
135                 stats->secret_count,
136                 stats->exported );
137       write_status_text (STATUS_EXPORT_RES, buf);
138     }
139 }
140
141
142 /*
143  * Export public keys (to stdout or to --output FILE).
144  *
145  * Depending on opt.armor the output is armored.  OPTIONS are defined
146  * in main.h.  If USERS is NULL, all keys will be exported.  STATS is
147  * either an export stats object for update or NULL.
148  *
149  * This function is the core of "gpg --export".
150  */
151 int
152 export_pubkeys (ctrl_t ctrl, strlist_t users, unsigned int options,
153                 export_stats_t stats)
154 {
155   return do_export (ctrl, users, 0, options, stats);
156 }
157
158
159 /*
160  * Export secret keys (to stdout or to --output FILE).
161  *
162  * Depending on opt.armor the output is armored.  If USERS is NULL,
163  * all secret keys will be exported.  STATS is either an export stats
164  * object for update or NULL.
165  *
166  * This function is the core of "gpg --export-secret-keys".
167  */
168 int
169 export_seckeys (ctrl_t ctrl, strlist_t users, export_stats_t stats)
170 {
171   return do_export (ctrl, users, 1, 0, stats);
172 }
173
174
175 /*
176  * Export secret sub keys (to stdout or to --output FILE).
177  *
178  * This is the same as export_seckeys but replaces the primary key by
179  * a stub key.  Depending on opt.armor the output is armored.  If
180  * USERS is NULL, all secret subkeys will be exported.  STATS is
181  * either an export stats object for update or NULL.
182  *
183  * This function is the core of "gpg --export-secret-subkeys".
184  */
185 int
186 export_secsubkeys (ctrl_t ctrl, strlist_t users, export_stats_t stats)
187 {
188   return do_export (ctrl, users, 2, 0, stats);
189 }
190
191
192 /*
193  * Export a single key into a memory buffer.  STATS is either an
194  * export stats object for update or NULL.
195  */
196 gpg_error_t
197 export_pubkey_buffer (ctrl_t ctrl, const char *keyspec, unsigned int options,
198                       export_stats_t stats,
199                       kbnode_t *r_keyblock, void **r_data, size_t *r_datalen)
200 {
201   gpg_error_t err;
202   iobuf_t iobuf;
203   int any;
204   strlist_t helplist;
205
206   *r_keyblock = NULL;
207   *r_data = NULL;
208   *r_datalen = 0;
209
210   helplist = NULL;
211   if (!add_to_strlist_try (&helplist, keyspec))
212     return gpg_error_from_syserror ();
213
214   iobuf = iobuf_temp ();
215   err = do_export_stream (ctrl, iobuf, helplist, 0, r_keyblock, options,
216                           stats, &any);
217   if (!err && !any)
218     err = gpg_error (GPG_ERR_NOT_FOUND);
219   if (!err)
220     {
221       const void *src;
222       size_t datalen;
223
224       iobuf_flush_temp (iobuf);
225       src = iobuf_get_temp_buffer (iobuf);
226       datalen = iobuf_get_temp_length (iobuf);
227       if (!datalen)
228         err = gpg_error (GPG_ERR_NO_PUBKEY);
229       else if (!(*r_data = xtrymalloc (datalen)))
230         err = gpg_error_from_syserror ();
231       else
232         {
233           memcpy (*r_data, src, datalen);
234           *r_datalen = datalen;
235         }
236     }
237   iobuf_close (iobuf);
238   free_strlist (helplist);
239   if (err && *r_keyblock)
240     {
241       release_kbnode (*r_keyblock);
242       *r_keyblock = NULL;
243     }
244   return err;
245 }
246
247
248 /* Export the keys identified by the list of strings in USERS.  If
249    Secret is false public keys will be exported.  With secret true
250    secret keys will be exported; in this case 1 means the entire
251    secret keyblock and 2 only the subkeys.  OPTIONS are the export
252    options to apply.  */
253 static int
254 do_export (ctrl_t ctrl, strlist_t users, int secret, unsigned int options,
255            export_stats_t stats)
256 {
257   IOBUF out = NULL;
258   int any, rc;
259   armor_filter_context_t *afx = NULL;
260   compress_filter_context_t zfx;
261
262   memset( &zfx, 0, sizeof zfx);
263
264   rc = open_outfile (-1, NULL, 0, !!secret, &out );
265   if (rc)
266     return rc;
267
268   if ( opt.armor )
269     {
270       afx = new_armor_context ();
271       afx->what = secret? 5 : 1;
272       push_armor_filter (afx, out);
273     }
274
275   rc = do_export_stream (ctrl, out, users, secret, NULL, options, stats, &any);
276
277   if ( rc || !any )
278     iobuf_cancel (out);
279   else
280     iobuf_close (out);
281   release_armor_context (afx);
282   return rc;
283 }
284
285
286
287 /* Release an entire subkey list. */
288 static void
289 release_subkey_list (subkey_list_t list)
290 {
291   while (list)
292     {
293       subkey_list_t tmp = list->next;;
294       xfree (list);
295       list = tmp;
296     }
297 }
298
299
300 /* Returns true if NODE is a subkey and contained in LIST. */
301 static int
302 subkey_in_list_p (subkey_list_t list, KBNODE node)
303 {
304   if (node->pkt->pkttype == PKT_PUBLIC_SUBKEY
305       || node->pkt->pkttype == PKT_SECRET_SUBKEY )
306     {
307       u32 kid[2];
308
309       keyid_from_pk (node->pkt->pkt.public_key, kid);
310
311       for (; list; list = list->next)
312         if (list->kid[0] == kid[0] && list->kid[1] == kid[1])
313           return 1;
314     }
315   return 0;
316 }
317
318 /* Allocate a new subkey list item from NODE. */
319 static subkey_list_t
320 new_subkey_list_item (KBNODE node)
321 {
322   subkey_list_t list = xcalloc (1, sizeof *list);
323
324   if (node->pkt->pkttype == PKT_PUBLIC_SUBKEY
325       || node->pkt->pkttype == PKT_SECRET_SUBKEY)
326     keyid_from_pk (node->pkt->pkt.public_key, list->kid);
327
328   return list;
329 }
330
331
332 /* Helper function to check whether the subkey at NODE actually
333    matches the description at DESC.  The function returns true if the
334    key under question has been specified by an exact specification
335    (keyID or fingerprint) and does match the one at NODE.  It is
336    assumed that the packet at NODE is either a public or secret
337    subkey. */
338 static int
339 exact_subkey_match_p (KEYDB_SEARCH_DESC *desc, KBNODE node)
340 {
341   u32 kid[2];
342   byte fpr[MAX_FINGERPRINT_LEN];
343   size_t fprlen;
344   int result = 0;
345
346   switch(desc->mode)
347     {
348     case KEYDB_SEARCH_MODE_SHORT_KID:
349     case KEYDB_SEARCH_MODE_LONG_KID:
350       keyid_from_pk (node->pkt->pkt.public_key, kid);
351       break;
352
353     case KEYDB_SEARCH_MODE_FPR16:
354     case KEYDB_SEARCH_MODE_FPR20:
355     case KEYDB_SEARCH_MODE_FPR:
356       fingerprint_from_pk (node->pkt->pkt.public_key, fpr,&fprlen);
357       break;
358
359     default:
360       break;
361     }
362
363   switch(desc->mode)
364     {
365     case KEYDB_SEARCH_MODE_SHORT_KID:
366       if (desc->u.kid[1] == kid[1])
367         result = 1;
368       break;
369
370     case KEYDB_SEARCH_MODE_LONG_KID:
371       if (desc->u.kid[0] == kid[0] && desc->u.kid[1] == kid[1])
372         result = 1;
373       break;
374
375     case KEYDB_SEARCH_MODE_FPR16:
376       if (!memcmp (desc->u.fpr, fpr, 16))
377         result = 1;
378       break;
379
380     case KEYDB_SEARCH_MODE_FPR20:
381     case KEYDB_SEARCH_MODE_FPR:
382       if (!memcmp (desc->u.fpr, fpr, 20))
383         result = 1;
384       break;
385
386     default:
387       break;
388     }
389
390   return result;
391 }
392
393
394 /* Return an error if the key represented by the S-expression S_KEY
395  * and the OpenPGP key represented by PK do not use the same curve. */
396 static gpg_error_t
397 match_curve_skey_pk (gcry_sexp_t s_key, PKT_public_key *pk)
398 {
399   gcry_sexp_t curve = NULL;
400   gcry_sexp_t flags = NULL;
401   char *curve_str = NULL;
402   char *flag;
403   const char *oidstr = NULL;
404   gcry_mpi_t curve_as_mpi = NULL;
405   gpg_error_t err;
406   int is_eddsa = 0;
407   int idx = 0;
408
409   if (!(pk->pubkey_algo==PUBKEY_ALGO_ECDH
410         || pk->pubkey_algo==PUBKEY_ALGO_ECDSA
411         || pk->pubkey_algo==PUBKEY_ALGO_EDDSA))
412     return gpg_error (GPG_ERR_PUBKEY_ALGO);
413
414   curve = gcry_sexp_find_token (s_key, "curve", 0);
415   if (!curve)
416     {
417       log_error ("no reported curve\n");
418       return gpg_error (GPG_ERR_UNKNOWN_CURVE);
419     }
420   curve_str = gcry_sexp_nth_string (curve, 1);
421   gcry_sexp_release (curve); curve = NULL;
422   if (!curve_str)
423     {
424       log_error ("no curve name\n");
425       return gpg_error (GPG_ERR_UNKNOWN_CURVE);
426     }
427   oidstr = openpgp_curve_to_oid (curve_str, NULL);
428   if (!oidstr)
429     {
430       log_error ("no OID known for curve '%s'\n", curve_str);
431       xfree (curve_str);
432       return gpg_error (GPG_ERR_UNKNOWN_CURVE);
433     }
434   xfree (curve_str);
435   err = openpgp_oid_from_str (oidstr, &curve_as_mpi);
436   if (err)
437     return err;
438   if (gcry_mpi_cmp (pk->pkey[0], curve_as_mpi))
439     {
440       log_error ("curves do not match\n");
441       gcry_mpi_release (curve_as_mpi);
442       return gpg_error (GPG_ERR_INV_CURVE);
443     }
444   gcry_mpi_release (curve_as_mpi);
445   flags = gcry_sexp_find_token (s_key, "flags", 0);
446   if (flags)
447     {
448       for (idx = 1; idx < gcry_sexp_length (flags); idx++)
449         {
450           flag = gcry_sexp_nth_string (flags, idx);
451           if (flag && (strcmp ("eddsa", flag) == 0))
452             is_eddsa = 1;
453           gcry_free (flag);
454         }
455     }
456   if (is_eddsa != (pk->pubkey_algo == PUBKEY_ALGO_EDDSA))
457     {
458       log_error ("disagreement about EdDSA\n");
459       err = gpg_error (GPG_ERR_INV_CURVE);
460     }
461
462   return err;
463 }
464
465
466 /* Return a canonicalized public key algoithms.  This is used to
467    compare different flavors of algorithms (e.g. ELG and ELG_E are
468    considered the same).  */
469 static enum gcry_pk_algos
470 canon_pk_algo (enum gcry_pk_algos algo)
471 {
472   switch (algo)
473     {
474     case GCRY_PK_RSA:
475     case GCRY_PK_RSA_E:
476     case GCRY_PK_RSA_S: return GCRY_PK_RSA;
477     case GCRY_PK_ELG:
478     case GCRY_PK_ELG_E: return GCRY_PK_ELG;
479     case GCRY_PK_ECC:
480     case GCRY_PK_ECDSA:
481     case GCRY_PK_ECDH: return GCRY_PK_ECC;
482     default: return algo;
483     }
484 }
485
486
487 /* Take a cleartext dump of a secret key in PK and change the
488  * parameter array in PK to include the secret parameters.  */
489 static gpg_error_t
490 cleartext_secret_key_to_openpgp (gcry_sexp_t s_key, PKT_public_key *pk)
491 {
492   gpg_error_t err = gpg_error (GPG_ERR_NOT_IMPLEMENTED);
493   gcry_sexp_t top_list;
494   gcry_sexp_t key = NULL;
495   char *key_type = NULL;
496   enum gcry_pk_algos pk_algo;
497   struct seckey_info *ski;
498   int idx, sec_start;
499   gcry_mpi_t pub_params[10] = { NULL };
500
501   /* we look for a private-key, then the first element in it tells us
502      the type */
503   top_list = gcry_sexp_find_token (s_key, "private-key", 0);
504   if (!top_list)
505     goto bad_seckey;
506   if (gcry_sexp_length(top_list) != 2)
507     goto bad_seckey;
508   key = gcry_sexp_nth (top_list, 1);
509   if (!key)
510     goto bad_seckey;
511   key_type = gcry_sexp_nth_string(key, 0);
512   pk_algo = gcry_pk_map_name (key_type);
513
514   log_assert (!pk->seckey_info);
515
516   pk->seckey_info = ski = xtrycalloc (1, sizeof *ski);
517   if (!ski)
518     {
519       err = gpg_error_from_syserror ();
520       goto leave;
521     }
522
523   switch (canon_pk_algo (pk_algo))
524     {
525     case GCRY_PK_RSA:
526       if (!is_RSA (pk->pubkey_algo))
527         goto bad_pubkey_algo;
528       err = gcry_sexp_extract_param (key, NULL, "ne",
529                                      &pub_params[0],
530                                      &pub_params[1],
531                                      NULL);
532       for (idx=0; idx < 2 && !err; idx++)
533         if (gcry_mpi_cmp(pk->pkey[idx], pub_params[idx]))
534           err = gpg_error (GPG_ERR_BAD_PUBKEY);
535       if (!err)
536         {
537           for (idx = 2; idx < 6 && !err; idx++)
538             {
539               gcry_mpi_release (pk->pkey[idx]);
540               pk->pkey[idx] = NULL;
541             }
542           err = gcry_sexp_extract_param (key, NULL, "dpqu",
543                                          &pk->pkey[2],
544                                          &pk->pkey[3],
545                                          &pk->pkey[4],
546                                          &pk->pkey[5],
547                                          NULL);
548         }
549       if (!err)
550         {
551           for (idx = 2; idx < 6; idx++)
552             ski->csum += checksum_mpi (pk->pkey[idx]);
553         }
554       break;
555
556     case GCRY_PK_DSA:
557       if (!is_DSA (pk->pubkey_algo))
558         goto bad_pubkey_algo;
559       err = gcry_sexp_extract_param (key, NULL, "pqgy",
560                                      &pub_params[0],
561                                      &pub_params[1],
562                                      &pub_params[2],
563                                      &pub_params[3],
564                                      NULL);
565       for (idx=0; idx < 4 && !err; idx++)
566         if (gcry_mpi_cmp(pk->pkey[idx], pub_params[idx]))
567           err = gpg_error (GPG_ERR_BAD_PUBKEY);
568       if (!err)
569         {
570           gcry_mpi_release (pk->pkey[4]);
571           pk->pkey[4] = NULL;
572           err = gcry_sexp_extract_param (key, NULL, "x",
573                                          &pk->pkey[4],
574                                          NULL);
575         }
576       if (!err)
577         ski->csum += checksum_mpi (pk->pkey[4]);
578       break;
579
580     case GCRY_PK_ELG:
581       if (!is_ELGAMAL (pk->pubkey_algo))
582         goto bad_pubkey_algo;
583       err = gcry_sexp_extract_param (key, NULL, "pgy",
584                                      &pub_params[0],
585                                      &pub_params[1],
586                                      &pub_params[2],
587                                      NULL);
588       for (idx=0; idx < 3 && !err; idx++)
589         if (gcry_mpi_cmp(pk->pkey[idx], pub_params[idx]))
590           err = gpg_error (GPG_ERR_BAD_PUBKEY);
591       if (!err)
592         {
593           gcry_mpi_release (pk->pkey[3]);
594           pk->pkey[3] = NULL;
595           err = gcry_sexp_extract_param (key, NULL, "x",
596                                          &pk->pkey[3],
597                                          NULL);
598         }
599       if (!err)
600         ski->csum += checksum_mpi (pk->pkey[3]);
601       break;
602
603     case GCRY_PK_ECC:
604       err = match_curve_skey_pk (key, pk);
605       if (err)
606         goto leave;
607       if (!err)
608         err = gcry_sexp_extract_param (key, NULL, "q",
609                                        &pub_params[0],
610                                        NULL);
611       if (!err && (gcry_mpi_cmp(pk->pkey[1], pub_params[0])))
612         err = gpg_error (GPG_ERR_BAD_PUBKEY);
613
614       sec_start = 2;
615       if (pk->pubkey_algo == PUBKEY_ALGO_ECDH)
616         sec_start += 1;
617       if (!err)
618         {
619           gcry_mpi_release (pk->pkey[sec_start]);
620           pk->pkey[sec_start] = NULL;
621           err = gcry_sexp_extract_param (key, NULL, "d",
622                                          &pk->pkey[sec_start],
623                                          NULL);
624         }
625
626       if (!err)
627         ski->csum += checksum_mpi (pk->pkey[sec_start]);
628       break;
629
630     default:
631       pk->seckey_info = NULL;
632       xfree (ski);
633       err = gpg_error (GPG_ERR_NOT_IMPLEMENTED);
634       break;
635     }
636
637  leave:
638   gcry_sexp_release (top_list);
639   gcry_sexp_release (key);
640   gcry_free (key_type);
641
642   for (idx=0; idx < DIM(pub_params); idx++)
643     gcry_mpi_release (pub_params[idx]);
644   return err;
645
646  bad_pubkey_algo:
647   err = gpg_error (GPG_ERR_PUBKEY_ALGO);
648   goto leave;
649
650  bad_seckey:
651   err = gpg_error (GPG_ERR_BAD_SECKEY);
652   goto leave;
653 }
654
655
656 /* Use the key transfer format given in S_PGP to create the secinfo
657    structure in PK and change the parameter array in PK to include the
658    secret parameters.  */
659 static gpg_error_t
660 transfer_format_to_openpgp (gcry_sexp_t s_pgp, PKT_public_key *pk)
661 {
662   gpg_error_t err;
663   gcry_sexp_t top_list;
664   gcry_sexp_t list = NULL;
665   char *curve = NULL;
666   const char *value;
667   size_t valuelen;
668   char *string;
669   int  idx;
670   int  is_v4, is_protected;
671   enum gcry_pk_algos pk_algo;
672   int  protect_algo = 0;
673   char iv[16];
674   int  ivlen = 0;
675   int  s2k_mode = 0;
676   int  s2k_algo = 0;
677   byte s2k_salt[8];
678   u32  s2k_count = 0;
679   int  is_ecdh = 0;
680   size_t npkey, nskey;
681   gcry_mpi_t skey[10];  /* We support up to 9 parameters.  */
682   int skeyidx = 0;
683   struct seckey_info *ski;
684
685   /* gcry_log_debugsxp ("transferkey", s_pgp); */
686   top_list = gcry_sexp_find_token (s_pgp, "openpgp-private-key", 0);
687   if (!top_list)
688     goto bad_seckey;
689
690   list = gcry_sexp_find_token (top_list, "version", 0);
691   if (!list)
692     goto bad_seckey;
693   value = gcry_sexp_nth_data (list, 1, &valuelen);
694   if (!value || valuelen != 1 || !(value[0] == '3' || value[0] == '4'))
695     goto bad_seckey;
696   is_v4 = (value[0] == '4');
697
698   gcry_sexp_release (list);
699   list = gcry_sexp_find_token (top_list, "protection", 0);
700   if (!list)
701     goto bad_seckey;
702   value = gcry_sexp_nth_data (list, 1, &valuelen);
703   if (!value)
704     goto bad_seckey;
705   if (valuelen == 4 && !memcmp (value, "sha1", 4))
706     is_protected = 2;
707   else if (valuelen == 3 && !memcmp (value, "sum", 3))
708     is_protected = 1;
709   else if (valuelen == 4 && !memcmp (value, "none", 4))
710     is_protected = 0;
711   else
712     goto bad_seckey;
713   if (is_protected)
714     {
715       string = gcry_sexp_nth_string (list, 2);
716       if (!string)
717         goto bad_seckey;
718       protect_algo = gcry_cipher_map_name (string);
719       xfree (string);
720
721       value = gcry_sexp_nth_data (list, 3, &valuelen);
722       if (!value || !valuelen || valuelen > sizeof iv)
723         goto bad_seckey;
724       memcpy (iv, value, valuelen);
725       ivlen = valuelen;
726
727       string = gcry_sexp_nth_string (list, 4);
728       if (!string)
729         goto bad_seckey;
730       s2k_mode = strtol (string, NULL, 10);
731       xfree (string);
732
733       string = gcry_sexp_nth_string (list, 5);
734       if (!string)
735         goto bad_seckey;
736       s2k_algo = gcry_md_map_name (string);
737       xfree (string);
738
739       value = gcry_sexp_nth_data (list, 6, &valuelen);
740       if (!value || !valuelen || valuelen > sizeof s2k_salt)
741         goto bad_seckey;
742       memcpy (s2k_salt, value, valuelen);
743
744       string = gcry_sexp_nth_string (list, 7);
745       if (!string)
746         goto bad_seckey;
747       s2k_count = strtoul (string, NULL, 10);
748       xfree (string);
749     }
750
751   /* Parse the gcrypt PK algo and check that it is okay.  */
752   gcry_sexp_release (list);
753   list = gcry_sexp_find_token (top_list, "algo", 0);
754   if (!list)
755     goto bad_seckey;
756   string = gcry_sexp_nth_string (list, 1);
757   if (!string)
758     goto bad_seckey;
759   pk_algo = gcry_pk_map_name (string);
760   xfree (string); string = NULL;
761   if (gcry_pk_algo_info (pk_algo, GCRYCTL_GET_ALGO_NPKEY, NULL, &npkey)
762       || gcry_pk_algo_info (pk_algo, GCRYCTL_GET_ALGO_NSKEY, NULL, &nskey)
763       || !npkey || npkey >= nskey)
764     goto bad_seckey;
765
766   /* Check that the pubkey algo matches the one from the public key.  */
767   switch (canon_pk_algo (pk_algo))
768     {
769     case GCRY_PK_RSA:
770       if (!is_RSA (pk->pubkey_algo))
771         pk_algo = 0;  /* Does not match.  */
772       break;
773     case GCRY_PK_DSA:
774       if (!is_DSA (pk->pubkey_algo))
775         pk_algo = 0;  /* Does not match.  */
776       break;
777     case GCRY_PK_ELG:
778       if (!is_ELGAMAL (pk->pubkey_algo))
779         pk_algo = 0;  /* Does not match.  */
780       break;
781     case GCRY_PK_ECC:
782       if (pk->pubkey_algo == PUBKEY_ALGO_ECDSA)
783         ;
784       else if (pk->pubkey_algo == PUBKEY_ALGO_ECDH)
785         is_ecdh = 1;
786       else if (pk->pubkey_algo == PUBKEY_ALGO_EDDSA)
787         ;
788       else
789         pk_algo = 0;  /* Does not match.  */
790       /* For ECC we do not have the domain parameters thus fix our info.  */
791       npkey = 1;
792       nskey = 2;
793       break;
794     default:
795       pk_algo = 0;   /* Oops.  */
796       break;
797     }
798   if (!pk_algo)
799     {
800       err = gpg_error (GPG_ERR_PUBKEY_ALGO);
801       goto leave;
802     }
803
804   /* This check has to go after the ecc adjustments. */
805   if (nskey > PUBKEY_MAX_NSKEY)
806     goto bad_seckey;
807
808   /* Parse the key parameters.  */
809   gcry_sexp_release (list);
810   list = gcry_sexp_find_token (top_list, "skey", 0);
811   if (!list)
812     goto bad_seckey;
813   for (idx=0;;)
814     {
815       int is_enc;
816
817       value = gcry_sexp_nth_data (list, ++idx, &valuelen);
818       if (!value && skeyidx >= npkey)
819         break;  /* Ready.  */
820
821       /* Check for too many parameters.  Note that depending on the
822          protection mode and version number we may see less than NSKEY
823          (but at least NPKEY+1) parameters.  */
824       if (idx >= 2*nskey)
825         goto bad_seckey;
826       if (skeyidx >= DIM (skey)-1)
827         goto bad_seckey;
828
829       if (!value || valuelen != 1 || !(value[0] == '_' || value[0] == 'e'))
830         goto bad_seckey;
831       is_enc = (value[0] == 'e');
832       value = gcry_sexp_nth_data (list, ++idx, &valuelen);
833       if (!value || !valuelen)
834         goto bad_seckey;
835       if (is_enc)
836         {
837           void *p = xtrymalloc (valuelen);
838           if (!p)
839             goto outofmem;
840           memcpy (p, value, valuelen);
841           skey[skeyidx] = gcry_mpi_set_opaque (NULL, p, valuelen*8);
842           if (!skey[skeyidx])
843             goto outofmem;
844         }
845       else
846         {
847           if (gcry_mpi_scan (skey + skeyidx, GCRYMPI_FMT_STD,
848                              value, valuelen, NULL))
849             goto bad_seckey;
850         }
851       skeyidx++;
852     }
853   skey[skeyidx++] = NULL;
854
855   gcry_sexp_release (list); list = NULL;
856
857   /* We have no need for the CSUM value thus we don't parse it.  */
858   /* list = gcry_sexp_find_token (top_list, "csum", 0); */
859   /* if (list) */
860   /*   { */
861   /*     string = gcry_sexp_nth_string (list, 1); */
862   /*     if (!string) */
863   /*       goto bad_seckey; */
864   /*     desired_csum = strtoul (string, NULL, 10); */
865   /*     xfree (string); */
866   /*   } */
867   /* else */
868   /*   desired_csum = 0; */
869   /* gcry_sexp_release (list); list = NULL; */
870
871   /* Get the curve name if any,  */
872   list = gcry_sexp_find_token (top_list, "curve", 0);
873   if (list)
874     {
875       curve = gcry_sexp_nth_string (list, 1);
876       gcry_sexp_release (list); list = NULL;
877     }
878
879   gcry_sexp_release (top_list); top_list = NULL;
880
881   /* log_debug ("XXX is_v4=%d\n", is_v4); */
882   /* log_debug ("XXX pubkey_algo=%d\n", pubkey_algo); */
883   /* log_debug ("XXX is_protected=%d\n", is_protected); */
884   /* log_debug ("XXX protect_algo=%d\n", protect_algo); */
885   /* log_printhex ("XXX iv", iv, ivlen); */
886   /* log_debug ("XXX ivlen=%d\n", ivlen); */
887   /* log_debug ("XXX s2k_mode=%d\n", s2k_mode); */
888   /* log_debug ("XXX s2k_algo=%d\n", s2k_algo); */
889   /* log_printhex ("XXX s2k_salt", s2k_salt, sizeof s2k_salt); */
890   /* log_debug ("XXX s2k_count=%lu\n", (unsigned long)s2k_count); */
891   /* for (idx=0; skey[idx]; idx++) */
892   /*   { */
893   /*     int is_enc = gcry_mpi_get_flag (skey[idx], GCRYMPI_FLAG_OPAQUE); */
894   /*     log_info ("XXX skey[%d]%s:", idx, is_enc? " (enc)":""); */
895   /*     if (is_enc) */
896   /*       { */
897   /*         void *p; */
898   /*         unsigned int nbits; */
899   /*         p = gcry_mpi_get_opaque (skey[idx], &nbits); */
900   /*         log_printhex (NULL, p, (nbits+7)/8); */
901   /*       } */
902   /*     else */
903   /*       gcry_mpi_dump (skey[idx]); */
904   /*     log_printf ("\n"); */
905   /*   } */
906
907   if (!is_v4 || is_protected != 2 )
908     {
909       /* We only support the v4 format and a SHA-1 checksum.  */
910       err = gpg_error (GPG_ERR_NOT_IMPLEMENTED);
911       goto leave;
912     }
913
914   /* We need to change the received parameters for ECC algorithms.
915      The transfer format has the curve name and the parameters
916      separate.  We put them all into the SKEY array.  */
917   if (canon_pk_algo (pk_algo) == GCRY_PK_ECC)
918     {
919       const char *oidstr;
920
921       /* Assert that all required parameters are available.  We also
922          check that the array does not contain more parameters than
923          needed (this was used by some beta versions of 2.1.  */
924       if (!curve || !skey[0] || !skey[1] || skey[2])
925         {
926           err = gpg_error (GPG_ERR_INTERNAL);
927           goto leave;
928         }
929
930       oidstr = openpgp_curve_to_oid (curve, NULL);
931       if (!oidstr)
932         {
933           log_error ("no OID known for curve '%s'\n", curve);
934           err = gpg_error (GPG_ERR_UNKNOWN_CURVE);
935           goto leave;
936         }
937       /* Put the curve's OID into into the MPI array.  This requires
938          that we shift Q and D.  For ECDH also insert the KDF parms. */
939       if (is_ecdh)
940         {
941           skey[4] = NULL;
942           skey[3] = skey[1];
943           skey[2] = gcry_mpi_copy (pk->pkey[2]);
944         }
945       else
946         {
947           skey[3] = NULL;
948           skey[2] = skey[1];
949         }
950       skey[1] = skey[0];
951       skey[0] = NULL;
952       err = openpgp_oid_from_str (oidstr, skey + 0);
953       if (err)
954         goto leave;
955       /* Fixup the NPKEY and NSKEY to match OpenPGP reality.  */
956       npkey = 2 + is_ecdh;
957       nskey = 3 + is_ecdh;
958
959       /* for (idx=0; skey[idx]; idx++) */
960       /*   { */
961       /*     log_info ("YYY skey[%d]:", idx); */
962       /*     if (gcry_mpi_get_flag (skey[idx], GCRYMPI_FLAG_OPAQUE)) */
963       /*       { */
964       /*         void *p; */
965       /*         unsigned int nbits; */
966       /*         p = gcry_mpi_get_opaque (skey[idx], &nbits); */
967       /*         log_printhex (NULL, p, (nbits+7)/8); */
968       /*       } */
969       /*     else */
970       /*       gcry_mpi_dump (skey[idx]); */
971       /*     log_printf ("\n"); */
972       /*   } */
973     }
974
975   /* Do some sanity checks.  */
976   if (s2k_count > 255)
977     {
978       /* We expect an already encoded S2K count.  */
979       err = gpg_error (GPG_ERR_INV_DATA);
980       goto leave;
981     }
982   err = openpgp_cipher_test_algo (protect_algo);
983   if (err)
984     goto leave;
985   err = openpgp_md_test_algo (s2k_algo);
986   if (err)
987     goto leave;
988
989   /* Check that the public key parameters match.  Note that since
990      Libgcrypt 1.5 gcry_mpi_cmp handles opaque MPI correctly.  */
991   for (idx=0; idx < npkey; idx++)
992     if (gcry_mpi_cmp (pk->pkey[idx], skey[idx]))
993       {
994         err = gpg_error (GPG_ERR_BAD_PUBKEY);
995         goto leave;
996       }
997
998   /* Check that the first secret key parameter in SKEY is encrypted
999      and that there are no more secret key parameters.  The latter is
1000      guaranteed by the v4 packet format.  */
1001   if (!gcry_mpi_get_flag (skey[npkey], GCRYMPI_FLAG_OPAQUE))
1002     goto bad_seckey;
1003   if (npkey+1 < DIM (skey) && skey[npkey+1])
1004     goto bad_seckey;
1005
1006   /* Check that the secret key parameters in PK are all set to NULL. */
1007   for (idx=npkey; idx < nskey; idx++)
1008     if (pk->pkey[idx])
1009       goto bad_seckey;
1010
1011   /* Now build the protection info. */
1012   pk->seckey_info = ski = xtrycalloc (1, sizeof *ski);
1013   if (!ski)
1014     {
1015       err = gpg_error_from_syserror ();
1016       goto leave;
1017     }
1018
1019   ski->is_protected = 1;
1020   ski->sha1chk = 1;
1021   ski->algo = protect_algo;
1022   ski->s2k.mode = s2k_mode;
1023   ski->s2k.hash_algo = s2k_algo;
1024   log_assert (sizeof ski->s2k.salt == sizeof s2k_salt);
1025   memcpy (ski->s2k.salt, s2k_salt, sizeof s2k_salt);
1026   ski->s2k.count = s2k_count;
1027   log_assert (ivlen <= sizeof ski->iv);
1028   memcpy (ski->iv, iv, ivlen);
1029   ski->ivlen = ivlen;
1030
1031   /* Store the protected secret key parameter.  */
1032   pk->pkey[npkey] = skey[npkey];
1033   skey[npkey] = NULL;
1034
1035   /* That's it.  */
1036
1037  leave:
1038   gcry_free (curve);
1039   gcry_sexp_release (list);
1040   gcry_sexp_release (top_list);
1041   for (idx=0; idx < skeyidx; idx++)
1042     gcry_mpi_release (skey[idx]);
1043   return err;
1044
1045  bad_seckey:
1046   err = gpg_error (GPG_ERR_BAD_SECKEY);
1047   goto leave;
1048
1049  outofmem:
1050   err = gpg_error (GPG_ERR_ENOMEM);
1051   goto leave;
1052 }
1053
1054
1055 /* Print an "EXPORTED" status line.  PK is the primary public key.  */
1056 static void
1057 print_status_exported (PKT_public_key *pk)
1058 {
1059   char *hexfpr;
1060
1061   if (!is_status_enabled ())
1062     return;
1063
1064   hexfpr = hexfingerprint (pk, NULL, 0);
1065   write_status_text (STATUS_EXPORTED, hexfpr? hexfpr : "[?]");
1066   xfree (hexfpr);
1067 }
1068
1069
1070 /*
1071  * Receive a secret key from agent specified by HEXGRIP.
1072  *
1073  * Since the key data from agant is encrypted, decrypt it by CIPHERHD.
1074  * Then, parse the decrypted key data in transfer format, and put
1075  * secret parameters into PK.
1076  *
1077  * If CLEARTEXT is 0, store the secret key material
1078  * passphrase-protected.  Otherwise, store secret key material in the
1079  * clear.
1080  *
1081  * CACHE_NONCE_ADDR is used to share nonce for multple key retrievals.
1082  */
1083 gpg_error_t
1084 receive_seckey_from_agent (ctrl_t ctrl, gcry_cipher_hd_t cipherhd,
1085                            int cleartext,
1086                            char **cache_nonce_addr, const char *hexgrip,
1087                            PKT_public_key *pk)
1088 {
1089   gpg_error_t err = 0;
1090   unsigned char *wrappedkey = NULL;
1091   size_t wrappedkeylen;
1092   unsigned char *key = NULL;
1093   size_t keylen, realkeylen;
1094   gcry_sexp_t s_skey;
1095   char *prompt;
1096
1097   if (opt.verbose)
1098     log_info ("key %s: asking agent for the secret parts\n", hexgrip);
1099
1100   prompt = gpg_format_keydesc (pk, FORMAT_KEYDESC_EXPORT,1);
1101   err = agent_export_key (ctrl, hexgrip, prompt, !cleartext, cache_nonce_addr,
1102                           &wrappedkey, &wrappedkeylen);
1103   xfree (prompt);
1104
1105   if (err)
1106     goto unwraperror;
1107   if (wrappedkeylen < 24)
1108     {
1109       err = gpg_error (GPG_ERR_INV_LENGTH);
1110       goto unwraperror;
1111     }
1112   keylen = wrappedkeylen - 8;
1113   key = xtrymalloc_secure (keylen);
1114   if (!key)
1115     {
1116       err = gpg_error_from_syserror ();
1117       goto unwraperror;
1118     }
1119   err = gcry_cipher_decrypt (cipherhd, key, keylen, wrappedkey, wrappedkeylen);
1120   if (err)
1121     goto unwraperror;
1122   realkeylen = gcry_sexp_canon_len (key, keylen, NULL, &err);
1123   if (!realkeylen)
1124     goto unwraperror; /* Invalid csexp.  */
1125
1126   err = gcry_sexp_sscan (&s_skey, NULL, key, realkeylen);
1127   if (!err)
1128     {
1129       if (cleartext)
1130         err = cleartext_secret_key_to_openpgp (s_skey, pk);
1131       else
1132         err = transfer_format_to_openpgp (s_skey, pk);
1133       gcry_sexp_release (s_skey);
1134     }
1135
1136  unwraperror:
1137   xfree (key);
1138   xfree (wrappedkey);
1139   if (err)
1140     {
1141       log_error ("key %s: error receiving key from agent:"
1142                  " %s%s\n", hexgrip, gpg_strerror (err),
1143                  gpg_err_code (err) == GPG_ERR_FULLY_CANCELED?
1144                  "":_(" - skipped"));
1145     }
1146   return err;
1147 }
1148
1149
1150 /* Export the keys identified by the list of strings in USERS to the
1151    stream OUT.  If Secret is false public keys will be exported.  With
1152    secret true secret keys will be exported; in this case 1 means the
1153    entire secret keyblock and 2 only the subkeys.  OPTIONS are the
1154    export options to apply.  If KEYBLOCK_OUT is not NULL, AND the exit
1155    code is zero, a pointer to the first keyblock found and exported
1156    will be stored at this address; no other keyblocks are exported in
1157    this case.  The caller must free the returned keyblock.  If any
1158    key has been exported true is stored at ANY. */
1159 static int
1160 do_export_stream (ctrl_t ctrl, iobuf_t out, strlist_t users, int secret,
1161                   kbnode_t *keyblock_out, unsigned int options,
1162                   export_stats_t stats, int *any)
1163 {
1164   gpg_error_t err = 0;
1165   PACKET pkt;
1166   KBNODE keyblock = NULL;
1167   KBNODE kbctx, node;
1168   size_t ndesc, descindex;
1169   KEYDB_SEARCH_DESC *desc = NULL;
1170   subkey_list_t subkey_list = NULL;  /* Track already processed subkeys. */
1171   KEYDB_HANDLE kdbhd;
1172   strlist_t sl;
1173   gcry_cipher_hd_t cipherhd = NULL;
1174   char *cache_nonce = NULL;
1175   struct export_stats_s dummystats;
1176   int cleartext = 0;
1177
1178   if (!stats)
1179     stats = &dummystats;
1180   *any = 0;
1181   init_packet (&pkt);
1182   kdbhd = keydb_new ();
1183   if (!kdbhd)
1184     return gpg_error_from_syserror ();
1185
1186   /* For the DANE format override the options.  */
1187   if ((options & EXPORT_DANE_FORMAT))
1188     options = (EXPORT_DANE_FORMAT | EXPORT_MINIMAL | EXPORT_CLEAN);
1189
1190
1191   if (!users)
1192     {
1193       ndesc = 1;
1194       desc = xcalloc (ndesc, sizeof *desc);
1195       desc[0].mode = KEYDB_SEARCH_MODE_FIRST;
1196     }
1197   else
1198     {
1199       for (ndesc=0, sl=users; sl; sl = sl->next, ndesc++)
1200         ;
1201       desc = xmalloc ( ndesc * sizeof *desc);
1202
1203       for (ndesc=0, sl=users; sl; sl = sl->next)
1204         {
1205           if (!(err=classify_user_id (sl->d, desc+ndesc, 1)))
1206             ndesc++;
1207           else
1208             log_error (_("key \"%s\" not found: %s\n"),
1209                        sl->d, gpg_strerror (err));
1210         }
1211
1212       keydb_disable_caching (kdbhd);  /* We are looping the search.  */
1213
1214       /* It would be nice to see which of the given users did actually
1215          match one in the keyring.  To implement this we need to have
1216          a found flag for each entry in desc.  To set this flag we
1217          must check all those entries after a match to mark all
1218          matched one - currently we stop at the first match.  To do
1219          this we need an extra flag to enable this feature.  */
1220     }
1221
1222 #ifdef ENABLE_SELINUX_HACKS
1223   if (secret)
1224     {
1225       log_error (_("exporting secret keys not allowed\n"));
1226       err = gpg_error (GPG_ERR_NOT_SUPPORTED);
1227       goto leave;
1228     }
1229 #endif
1230
1231   /* For secret key export we need to setup a decryption context.  */
1232   if (secret)
1233     {
1234       void *kek = NULL;
1235       size_t keklen;
1236
1237       err = agent_keywrap_key (ctrl, 1, &kek, &keklen);
1238       if (err)
1239         {
1240           log_error ("error getting the KEK: %s\n", gpg_strerror (err));
1241           goto leave;
1242         }
1243
1244       /* Prepare a cipher context.  */
1245       err = gcry_cipher_open (&cipherhd, GCRY_CIPHER_AES128,
1246                               GCRY_CIPHER_MODE_AESWRAP, 0);
1247       if (!err)
1248         err = gcry_cipher_setkey (cipherhd, kek, keklen);
1249       if (err)
1250         {
1251           log_error ("error setting up an encryption context: %s\n",
1252                      gpg_strerror (err));
1253           goto leave;
1254         }
1255       xfree (kek);
1256       kek = NULL;
1257     }
1258
1259   for (;;)
1260     {
1261       int skip_until_subkey = 0;
1262       u32 keyid[2];
1263       PKT_public_key *pk;
1264
1265       err = keydb_search (kdbhd, desc, ndesc, &descindex);
1266       if (!users)
1267         desc[0].mode = KEYDB_SEARCH_MODE_NEXT;
1268       if (err)
1269         break;
1270
1271       /* Read the keyblock. */
1272       release_kbnode (keyblock);
1273       keyblock = NULL;
1274       err = keydb_get_keyblock (kdbhd, &keyblock);
1275       if (err)
1276         {
1277           log_error (_("error reading keyblock: %s\n"), gpg_strerror (err));
1278           goto leave;
1279         }
1280
1281       node = find_kbnode (keyblock, PKT_PUBLIC_KEY);
1282       if (!node)
1283         {
1284           log_error ("public key packet not found in keyblock - skipped\n");
1285           continue;
1286         }
1287       stats->count++;
1288       setup_main_keyids (keyblock);  /* gpg_format_keydesc needs it.  */
1289       pk = node->pkt->pkt.public_key;
1290       keyid_from_pk (pk, keyid);
1291
1292       /* If a secret key export is required we need to check whether
1293          we have a secret key at all and if so create the seckey_info
1294          structure.  */
1295       if (secret)
1296         {
1297           if (agent_probe_any_secret_key (ctrl, keyblock))
1298             continue;  /* No secret key (neither primary nor subkey).  */
1299
1300           /* No v3 keys with GNU mode 1001. */
1301           if (secret == 2 && pk->version == 3)
1302             {
1303               log_info (_("key %s: PGP 2.x style key - skipped\n"),
1304                         keystr (keyid));
1305               continue;
1306             }
1307
1308           /* The agent does not yet allow to export v3 packets.  It is
1309              actually questionable whether we should allow them at
1310              all.  */
1311           if (pk->version == 3)
1312             {
1313               log_info ("key %s: PGP 2.x style key (v3) export "
1314                         "not yet supported - skipped\n", keystr (keyid));
1315               continue;
1316             }
1317           stats->secret_count++;
1318         }
1319
1320       /* Always do the cleaning on the public key part if requested.
1321          Note that we don't yet set this option if we are exporting
1322          secret keys.  Note that both export-clean and export-minimal
1323          only apply to UID sigs (0x10, 0x11, 0x12, and 0x13).  A
1324          designated revocation is never stripped, even with
1325          export-minimal set.  */
1326       if ((options & EXPORT_CLEAN))
1327         clean_key (keyblock, opt.verbose, (options&EXPORT_MINIMAL), NULL, NULL);
1328
1329       /* And write it. */
1330       xfree (cache_nonce);
1331       cache_nonce = NULL;
1332       for (kbctx=NULL; (node = walk_kbnode (keyblock, &kbctx, 0)); )
1333         {
1334           if (skip_until_subkey)
1335             {
1336               if (node->pkt->pkttype == PKT_PUBLIC_SUBKEY)
1337                 skip_until_subkey = 0;
1338               else
1339                 continue;
1340             }
1341
1342           /* We used to use comment packets, but not any longer.  In
1343              case we still have comments on a key, strip them here
1344              before we call build_packet(). */
1345           if (node->pkt->pkttype == PKT_COMMENT)
1346             continue;
1347
1348           /* Make sure that ring_trust packets never get exported. */
1349           if (node->pkt->pkttype == PKT_RING_TRUST)
1350             continue;
1351
1352           /* If exact is set, then we only export what was requested
1353              (plus the primary key, if the user didn't specifically
1354              request it). */
1355           if (desc[descindex].exact
1356               && node->pkt->pkttype == PKT_PUBLIC_SUBKEY)
1357             {
1358               if (!exact_subkey_match_p (desc+descindex, node))
1359                 {
1360                   /* Before skipping this subkey, check whether any
1361                      other description wants an exact match on a
1362                      subkey and include that subkey into the output
1363                      too.  Need to add this subkey to a list so that
1364                      it won't get processed a second time.
1365
1366                      So the first step here is to check that list and
1367                      skip in any case if the key is in that list.
1368
1369                      We need this whole mess because the import
1370                      function of GnuPG < 2.1 is not able to merge
1371                      secret keys and thus it is useless to output them
1372                      as two separate keys and have import merge them.  */
1373                   if (subkey_in_list_p (subkey_list, node))
1374                     skip_until_subkey = 1; /* Already processed this one. */
1375                   else
1376                     {
1377                       size_t j;
1378
1379                       for (j=0; j < ndesc; j++)
1380                         if (j != descindex && desc[j].exact
1381                             && exact_subkey_match_p (desc+j, node))
1382                           break;
1383                       if (!(j < ndesc))
1384                         skip_until_subkey = 1; /* No other one matching. */
1385                     }
1386                 }
1387
1388               if(skip_until_subkey)
1389                 continue;
1390
1391               /* Mark this one as processed. */
1392               {
1393                 subkey_list_t tmp = new_subkey_list_item (node);
1394                 tmp->next = subkey_list;
1395                 subkey_list = tmp;
1396               }
1397             }
1398
1399           if (node->pkt->pkttype == PKT_SIGNATURE)
1400             {
1401               /* Do not export packets which are marked as not
1402                  exportable.  */
1403               if (!(options&EXPORT_LOCAL_SIGS)
1404                   && !node->pkt->pkt.signature->flags.exportable)
1405                 continue; /* not exportable */
1406
1407               /* Do not export packets with a "sensitive" revocation
1408                  key unless the user wants us to.  Note that we do
1409                  export these when issuing the actual revocation
1410                  (see revoke.c). */
1411               if (!(options&EXPORT_SENSITIVE_REVKEYS)
1412                   && node->pkt->pkt.signature->revkey)
1413                 {
1414                   int i;
1415
1416                   for (i=0;i<node->pkt->pkt.signature->numrevkeys;i++)
1417                     if ( (node->pkt->pkt.signature->revkey[i].class & 0x40))
1418                       break;
1419
1420                   if (i < node->pkt->pkt.signature->numrevkeys)
1421                     continue;
1422                 }
1423             }
1424
1425           /* Don't export attribs? */
1426           if (!(options&EXPORT_ATTRIBUTES)
1427               && node->pkt->pkttype == PKT_USER_ID
1428               && node->pkt->pkt.user_id->attrib_data )
1429             {
1430               /* Skip until we get to something that is not an attrib
1431                  or a signature on an attrib */
1432               while (kbctx->next && kbctx->next->pkt->pkttype==PKT_SIGNATURE)
1433                 kbctx = kbctx->next;
1434
1435               continue;
1436             }
1437
1438           if (secret && (node->pkt->pkttype == PKT_PUBLIC_KEY
1439                          || node->pkt->pkttype == PKT_PUBLIC_SUBKEY))
1440             {
1441               u32 subkidbuf[2], *subkid;
1442               char *hexgrip, *serialno;
1443
1444               pk = node->pkt->pkt.public_key;
1445               if (node->pkt->pkttype == PKT_PUBLIC_KEY)
1446                 subkid = NULL;
1447               else
1448                 {
1449                   keyid_from_pk (pk, subkidbuf);
1450                   subkid = subkidbuf;
1451                 }
1452
1453               if (pk->seckey_info)
1454                 {
1455                   log_error ("key %s: oops: seckey_info already set"
1456                              " - skipped\n", keystr_with_sub (keyid, subkid));
1457                   skip_until_subkey = 1;
1458                   continue;
1459                 }
1460
1461               err = hexkeygrip_from_pk (pk, &hexgrip);
1462               if (err)
1463                 {
1464                   log_error ("key %s: error computing keygrip: %s"
1465                              " - skipped\n", keystr_with_sub (keyid, subkid),
1466                              gpg_strerror (err));
1467                   skip_until_subkey = 1;
1468                   err = 0;
1469                   continue;
1470                 }
1471
1472               if (secret == 2 && node->pkt->pkttype == PKT_PUBLIC_KEY)
1473                 {
1474                   /* We are asked not to export the secret parts of
1475                      the primary key.  Make up an error code to create
1476                      the stub.  */
1477                   err = GPG_ERR_NOT_FOUND;
1478                   serialno = NULL;
1479                 }
1480               else
1481                 err = agent_get_keyinfo (ctrl, hexgrip, &serialno, &cleartext);
1482
1483               if ((!err && serialno)
1484                   && secret == 2 && node->pkt->pkttype == PKT_PUBLIC_KEY)
1485                 {
1486                   /* It does not make sense to export a key with its
1487                      primary key on card using a non-key stub.  Thus
1488                      we skip those keys when used with
1489                      --export-secret-subkeys. */
1490                   log_info (_("key %s: key material on-card - skipped\n"),
1491                             keystr_with_sub (keyid, subkid));
1492                   skip_until_subkey = 1;
1493                 }
1494               else if (gpg_err_code (err) == GPG_ERR_NOT_FOUND
1495                        || (!err && serialno))
1496                 {
1497                   /* Create a key stub.  */
1498                   struct seckey_info *ski;
1499                   const char *s;
1500
1501                   pk->seckey_info = ski = xtrycalloc (1, sizeof *ski);
1502                   if (!ski)
1503                     {
1504                       err = gpg_error_from_syserror ();
1505                       xfree (hexgrip);
1506                       goto leave;
1507                     }
1508
1509                   ski->is_protected = 1;
1510                   if (err)
1511                     ski->s2k.mode = 1001; /* GNU dummy (no secret key).  */
1512                   else
1513                     {
1514                       ski->s2k.mode = 1002; /* GNU-divert-to-card.  */
1515                       for (s=serialno; sizeof (ski->ivlen) && *s && s[1];
1516                            ski->ivlen++, s += 2)
1517                         ski->iv[ski->ivlen] = xtoi_2 (s);
1518                     }
1519
1520                   err = build_packet (out, node->pkt);
1521                   if (!err && node->pkt->pkttype == PKT_PUBLIC_KEY)
1522                     {
1523                       stats->exported++;
1524                       print_status_exported (node->pkt->pkt.public_key);
1525                     }
1526                 }
1527               else if (!err)
1528                 {
1529                   err = receive_seckey_from_agent (ctrl, cipherhd,
1530                                                    cleartext, &cache_nonce,
1531                                                    hexgrip, pk);
1532                   if (err)
1533                     {
1534                       if (gpg_err_code (err) == GPG_ERR_FULLY_CANCELED)
1535                         goto leave;
1536                       skip_until_subkey = 1;
1537                       err = 0;
1538                     }
1539                   else
1540                     {
1541                       err = build_packet (out, node->pkt);
1542                       if (node->pkt->pkttype == PKT_PUBLIC_KEY)
1543                         {
1544                           stats->exported++;
1545                           print_status_exported (node->pkt->pkt.public_key);
1546                         }
1547                     }
1548                 }
1549               else
1550                 {
1551                   log_error ("key %s: error getting keyinfo from agent: %s"
1552                              " - skipped\n", keystr_with_sub (keyid, subkid),
1553                              gpg_strerror (err));
1554                   skip_until_subkey = 1;
1555                   err = 0;
1556                 }
1557
1558               xfree (pk->seckey_info);
1559               pk->seckey_info = NULL;
1560               xfree (hexgrip);
1561             }
1562           else
1563             {
1564               err = build_packet (out, node->pkt);
1565               if (!err && node->pkt->pkttype == PKT_PUBLIC_KEY)
1566                 {
1567                   stats->exported++;
1568                   print_status_exported (node->pkt->pkt.public_key);
1569                 }
1570             }
1571
1572
1573           if (err)
1574             {
1575               log_error ("build_packet(%d) failed: %s\n",
1576                          node->pkt->pkttype, gpg_strerror (err));
1577               goto leave;
1578             }
1579
1580           if (!skip_until_subkey)
1581             *any = 1;
1582         }
1583
1584       if (keyblock_out)
1585         {
1586           *keyblock_out = keyblock;
1587           break;
1588         }
1589     }
1590   if (gpg_err_code (err) == GPG_ERR_NOT_FOUND)
1591     err = 0;
1592
1593  leave:
1594   gcry_cipher_close (cipherhd);
1595   release_subkey_list (subkey_list);
1596   xfree(desc);
1597   keydb_release (kdbhd);
1598   if (err || !keyblock_out)
1599     release_kbnode( keyblock );
1600   xfree (cache_nonce);
1601   if( !*any )
1602     log_info(_("WARNING: nothing exported\n"));
1603   return err;
1604 }
1605
1606
1607
1608 \f
1609 static gpg_error_t
1610 key_to_sshblob (membuf_t *mb, const char *identifier, ...)
1611 {
1612   va_list arg_ptr;
1613   gpg_error_t err = 0;
1614   unsigned char nbuf[4];
1615   unsigned char *buf;
1616   size_t buflen;
1617   gcry_mpi_t a;
1618
1619   ulongtobuf (nbuf, (ulong)strlen (identifier));
1620   put_membuf (mb, nbuf, 4);
1621   put_membuf_str (mb, identifier);
1622   if (!strncmp (identifier, "ecdsa-sha2-", 11))
1623     {
1624       ulongtobuf (nbuf, (ulong)strlen (identifier+11));
1625       put_membuf (mb, nbuf, 4);
1626       put_membuf_str (mb, identifier+11);
1627     }
1628   va_start (arg_ptr, identifier);
1629   while ((a = va_arg (arg_ptr, gcry_mpi_t)))
1630     {
1631       err = gcry_mpi_aprint (GCRYMPI_FMT_SSH, &buf, &buflen, a);
1632       if (err)
1633         break;
1634       if (!strcmp (identifier, "ssh-ed25519")
1635           && buflen > 5 && buf[4] == 0x40)
1636         {
1637           /* We need to strip our 0x40 prefix.  */
1638           put_membuf (mb, "\x00\x00\x00\x20", 4);
1639           put_membuf (mb, buf+5, buflen-5);
1640         }
1641       else
1642         put_membuf (mb, buf, buflen);
1643       gcry_free (buf);
1644     }
1645   va_end (arg_ptr);
1646   return err;
1647 }
1648
1649 /* Export the key identified by USERID in the SSH public key format.
1650    The function exports the latest subkey with Authentication
1651    capability unless the '!' suffix is used to export a specific
1652    key.  */
1653 gpg_error_t
1654 export_ssh_key (ctrl_t ctrl, const char *userid)
1655 {
1656   gpg_error_t err;
1657   kbnode_t keyblock = NULL;
1658   KEYDB_SEARCH_DESC desc;
1659   u32 latest_date;
1660   u32 curtime = make_timestamp ();
1661   kbnode_t latest_key, node;
1662   PKT_public_key *pk;
1663   const char *identifier;
1664   membuf_t mb;
1665   estream_t fp = NULL;
1666   struct b64state b64_state;
1667   const char *fname = "-";
1668
1669   init_membuf (&mb, 4096);
1670
1671   /* We need to know whether the key has been specified using the
1672      exact syntax ('!' suffix).  Thus we need to run a
1673      classify_user_id on our own.  */
1674   err = classify_user_id (userid, &desc, 1);
1675
1676   /* Get the public key.  */
1677   if (!err)
1678     {
1679       getkey_ctx_t getkeyctx;
1680
1681       err = get_pubkey_byname (ctrl, &getkeyctx, NULL, userid, &keyblock,
1682                                NULL,
1683                                0  /* Only usable keys or given exact. */,
1684                                1  /* No AKL lookup.  */);
1685       if (!err)
1686         {
1687           err = getkey_next (getkeyctx, NULL, NULL);
1688           if (!err)
1689             err = gpg_error (GPG_ERR_AMBIGUOUS_NAME);
1690           else if (gpg_err_code (err) == GPG_ERR_NO_PUBKEY)
1691             err = 0;
1692         }
1693       getkey_end (getkeyctx);
1694     }
1695   if (err)
1696     {
1697       log_error (_("key \"%s\" not found: %s\n"), userid, gpg_strerror (err));
1698       return err;
1699     }
1700
1701   /* The finish_lookup code in getkey.c does not handle auth keys,
1702      thus we have to duplicate the code here to find the latest
1703      subkey.  However, if the key has been found using an exact match
1704      ('!' notation) we use that key without any further checks and
1705      even allow the use of the primary key. */
1706   latest_date = 0;
1707   latest_key = NULL;
1708   for (node = keyblock; node; node = node->next)
1709     {
1710       if ((node->pkt->pkttype == PKT_PUBLIC_SUBKEY
1711            || node->pkt->pkttype == PKT_PUBLIC_KEY)
1712           && node->pkt->pkt.public_key->flags.exact)
1713         {
1714           latest_key = node;
1715           break;
1716         }
1717     }
1718   if (!latest_key)
1719     {
1720       for (node = keyblock; node; node = node->next)
1721         {
1722           if (node->pkt->pkttype != PKT_PUBLIC_SUBKEY)
1723             continue;
1724
1725           pk = node->pkt->pkt.public_key;
1726           if (DBG_LOOKUP)
1727             log_debug ("\tchecking subkey %08lX\n",
1728                        (ulong) keyid_from_pk (pk, NULL));
1729           if (!(pk->pubkey_usage & PUBKEY_USAGE_AUTH))
1730             {
1731               if (DBG_LOOKUP)
1732                 log_debug ("\tsubkey not usable for authentication\n");
1733               continue;
1734             }
1735           if (!pk->flags.valid)
1736             {
1737               if (DBG_LOOKUP)
1738                 log_debug ("\tsubkey not valid\n");
1739               continue;
1740             }
1741           if (pk->flags.revoked)
1742             {
1743               if (DBG_LOOKUP)
1744                 log_debug ("\tsubkey has been revoked\n");
1745               continue;
1746             }
1747           if (pk->has_expired)
1748             {
1749               if (DBG_LOOKUP)
1750                 log_debug ("\tsubkey has expired\n");
1751               continue;
1752             }
1753           if (pk->timestamp > curtime && !opt.ignore_valid_from)
1754             {
1755               if (DBG_LOOKUP)
1756                 log_debug ("\tsubkey not yet valid\n");
1757               continue;
1758             }
1759           if (DBG_LOOKUP)
1760             log_debug ("\tsubkey might be fine\n");
1761           /* In case a key has a timestamp of 0 set, we make sure that it
1762              is used.  A better change would be to compare ">=" but that
1763              might also change the selected keys and is as such a more
1764              intrusive change.  */
1765           if (pk->timestamp > latest_date || (!pk->timestamp && !latest_date))
1766             {
1767               latest_date = pk->timestamp;
1768               latest_key = node;
1769             }
1770         }
1771     }
1772
1773   if (!latest_key)
1774     {
1775       err = gpg_error (GPG_ERR_UNUSABLE_PUBKEY);
1776       log_error (_("key \"%s\" not found: %s\n"), userid, gpg_strerror (err));
1777       goto leave;
1778     }
1779
1780   pk = latest_key->pkt->pkt.public_key;
1781   if (DBG_LOOKUP)
1782     log_debug ("\tusing key %08lX\n", (ulong) keyid_from_pk (pk, NULL));
1783
1784   switch (pk->pubkey_algo)
1785     {
1786     case PUBKEY_ALGO_DSA:
1787       identifier = "ssh-dss";
1788       err = key_to_sshblob (&mb, identifier,
1789                             pk->pkey[0], pk->pkey[1], pk->pkey[2], pk->pkey[3],
1790                             NULL);
1791       break;
1792
1793     case PUBKEY_ALGO_RSA:
1794     case PUBKEY_ALGO_RSA_S:
1795       identifier = "ssh-rsa";
1796       err = key_to_sshblob (&mb, identifier, pk->pkey[1], pk->pkey[0], NULL);
1797       break;
1798
1799     case PUBKEY_ALGO_ECDSA:
1800       {
1801         char *curveoid;
1802         const char *curve;
1803
1804         curveoid = openpgp_oid_to_str (pk->pkey[0]);
1805         if (!curveoid)
1806           err = gpg_error_from_syserror ();
1807         else if (!(curve = openpgp_oid_to_curve (curveoid, 0)))
1808           err = gpg_error (GPG_ERR_UNKNOWN_CURVE);
1809         else
1810           {
1811             if (!strcmp (curve, "nistp256"))
1812               identifier = "ecdsa-sha2-nistp256";
1813             else if (!strcmp (curve, "nistp384"))
1814               identifier = "ecdsa-sha2-nistp384";
1815             else if (!strcmp (curve, "nistp521"))
1816               identifier = "ecdsa-sha2-nistp521";
1817             else
1818               identifier = NULL;
1819
1820             if (!identifier)
1821               err = gpg_error (GPG_ERR_UNKNOWN_CURVE);
1822             else
1823               err = key_to_sshblob (&mb, identifier, pk->pkey[1], NULL);
1824           }
1825         xfree (curveoid);
1826       }
1827       break;
1828
1829     case PUBKEY_ALGO_EDDSA:
1830       if (!openpgp_oid_is_ed25519 (pk->pkey[0]))
1831         err = gpg_error (GPG_ERR_UNKNOWN_CURVE);
1832       else
1833         {
1834           identifier = "ssh-ed25519";
1835           err = key_to_sshblob (&mb, identifier, pk->pkey[1], NULL);
1836         }
1837       break;
1838
1839     case PUBKEY_ALGO_ELGAMAL_E:
1840     case PUBKEY_ALGO_ELGAMAL:
1841       err = gpg_error (GPG_ERR_UNUSABLE_PUBKEY);
1842       break;
1843
1844     default:
1845       err = GPG_ERR_PUBKEY_ALGO;
1846       break;
1847     }
1848
1849   if (err)
1850     goto leave;
1851
1852   if (opt.outfile && *opt.outfile && strcmp (opt.outfile, "-"))
1853     fp = es_fopen ((fname = opt.outfile), "w");
1854   else
1855     fp = es_stdout;
1856   if (!fp)
1857     {
1858       err = gpg_error_from_syserror ();
1859       log_error (_("error creating '%s': %s\n"), fname, gpg_strerror (err));
1860       goto leave;
1861     }
1862
1863   es_fprintf (fp, "%s ", identifier);
1864   err = b64enc_start_es (&b64_state, fp, "");
1865   if (err)
1866     goto leave;
1867   {
1868     void *blob;
1869     size_t bloblen;
1870
1871     blob = get_membuf (&mb, &bloblen);
1872     if (!blob)
1873       err = gpg_error_from_syserror ();
1874     else
1875       err = b64enc_write (&b64_state, blob, bloblen);
1876     xfree (blob);
1877     if (err)
1878       goto leave;
1879   }
1880   err = b64enc_finish (&b64_state);
1881   if (err)
1882     goto leave;
1883   es_fprintf (fp, " openpgp:0x%08lX\n", (ulong)keyid_from_pk (pk, NULL));
1884
1885   if (es_ferror (fp))
1886     err = gpg_error_from_syserror ();
1887   else
1888     {
1889       if (es_fclose (fp))
1890         err = gpg_error_from_syserror ();
1891       fp = NULL;
1892     }
1893
1894   if (err)
1895     log_error (_("error writing '%s': %s\n"), fname, gpg_strerror (err));
1896
1897  leave:
1898   es_fclose (fp);
1899   xfree (get_membuf (&mb, NULL));
1900   release_kbnode (keyblock);
1901   return err;
1902 }