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.
5 * This file is part of GnuPG.
7 * GnuPG is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 3 of the License, or
10 * (at your option) any later version.
12 * GnuPG is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, see <http://www.gnu.org/licenses/>.
37 #include "call-agent.h"
39 /* An object to keep track of subkeys. */
42 struct subkey_list_s *next;
45 typedef struct subkey_list_s *subkey_list_t;
48 static int do_export (ctrl_t ctrl,
49 strlist_t users, int secret, unsigned int options );
50 static int do_export_stream (ctrl_t ctrl, iobuf_t out,
51 strlist_t users, int secret,
52 kbnode_t *keyblock_out, unsigned int options,
54 static int build_sexp (iobuf_t out, PACKET *pkt, int *indent);
58 parse_export_options(char *str,unsigned int *options,int noisy)
60 struct parse_options export_opts[]=
62 {"export-local-sigs",EXPORT_LOCAL_SIGS,NULL,
63 N_("export signatures that are marked as local-only")},
64 {"export-attributes",EXPORT_ATTRIBUTES,NULL,
65 N_("export attribute user IDs (generally photo IDs)")},
66 {"export-sensitive-revkeys",EXPORT_SENSITIVE_REVKEYS,NULL,
67 N_("export revocation keys marked as \"sensitive\"")},
68 {"export-clean",EXPORT_CLEAN,NULL,
69 N_("remove unusable parts from key during export")},
70 {"export-minimal",EXPORT_MINIMAL|EXPORT_CLEAN,NULL,
71 N_("remove as much as possible from key during export")},
72 {"export-sexp-format",EXPORT_SEXP_FORMAT, NULL,
73 N_("export keys in an S-expression based format")},
74 /* Aliases for backward compatibility */
75 {"include-local-sigs",EXPORT_LOCAL_SIGS,NULL,NULL},
76 {"include-attributes",EXPORT_ATTRIBUTES,NULL,NULL},
77 {"include-sensitive-revkeys",EXPORT_SENSITIVE_REVKEYS,NULL,NULL},
79 {"export-unusable-sigs",0,NULL,NULL},
80 {"export-clean-sigs",0,NULL,NULL},
81 {"export-clean-uids",0,NULL,NULL},
83 /* add tags for include revoked and disabled? */
86 return parse_options(str,options,export_opts,noisy);
91 * Export the public keys (to standard out or --output).
92 * Depending on opt.armor the output is armored.
93 * options are defined in main.h.
94 * If USERS is NULL, the complete ring will be exported. */
96 export_pubkeys (ctrl_t ctrl, strlist_t users, unsigned int options )
98 return do_export (ctrl, users, 0, options );
102 * Export to an already opened stream; return -1 if no keys have
106 export_pubkeys_stream (ctrl_t ctrl, iobuf_t out, strlist_t users,
107 kbnode_t *keyblock_out, unsigned int options )
111 rc = do_export_stream (ctrl, out, users, 0, keyblock_out, options, &any);
119 * Export a single key into a memory buffer.
122 export_pubkey_buffer (ctrl_t ctrl, const char *keyspec, unsigned int options,
123 kbnode_t *r_keyblock, void **r_data, size_t *r_datalen)
135 if (!add_to_strlist_try (&helplist, keyspec))
136 return gpg_error_from_syserror ();
138 iobuf = iobuf_temp ();
139 err = do_export_stream (ctrl, iobuf, helplist, 0, r_keyblock, options, &any);
141 err = gpg_error (GPG_ERR_NOT_FOUND);
147 iobuf_flush_temp (iobuf);
148 src = iobuf_get_temp_buffer (iobuf);
149 datalen = iobuf_get_temp_length (iobuf);
151 err = gpg_error (GPG_ERR_NO_PUBKEY);
152 else if (!(*r_data = xtrymalloc (datalen)))
153 err = gpg_error_from_syserror ();
156 memcpy (*r_data, src, datalen);
157 *r_datalen = datalen;
161 free_strlist (helplist);
162 if (err && *r_keyblock)
164 release_kbnode (*r_keyblock);
172 export_seckeys (ctrl_t ctrl, strlist_t users )
174 /* Use only relevant options for the secret key. */
175 unsigned int options = (opt.export_options & EXPORT_SEXP_FORMAT);
176 return do_export (ctrl, users, 1, options);
180 export_secsubkeys (ctrl_t ctrl, strlist_t users )
182 /* Use only relevant options for the secret key. */
183 unsigned int options = (opt.export_options & EXPORT_SEXP_FORMAT);
184 return do_export (ctrl, users, 2, options);
188 /* Export the keys identified by the list of strings in USERS. If
189 Secret is false public keys will be exported. With secret true
190 secret keys will be exported; in this case 1 means the entire
191 secret keyblock and 2 only the subkeys. OPTIONS are the export
194 do_export (ctrl_t ctrl, strlist_t users, int secret, unsigned int options )
198 armor_filter_context_t *afx = NULL;
199 compress_filter_context_t zfx;
201 memset( &zfx, 0, sizeof zfx);
203 rc = open_outfile (GNUPG_INVALID_FD, NULL, 0, &out );
207 if (!(options & EXPORT_SEXP_FORMAT))
211 afx = new_armor_context ();
212 afx->what = secret? 5 : 1;
213 push_armor_filter (afx, out);
215 if ( opt.compress_keys )
216 push_compress_filter (out,&zfx,default_compress_algo());
219 rc = do_export_stream (ctrl, out, users, secret, NULL, options, &any );
225 release_armor_context (afx);
231 /* Release an entire subkey list. */
233 release_subkey_list (subkey_list_t list)
237 subkey_list_t tmp = list->next;;
244 /* Returns true if NODE is a subkey and contained in LIST. */
246 subkey_in_list_p (subkey_list_t list, KBNODE node)
248 if (node->pkt->pkttype == PKT_PUBLIC_SUBKEY
249 || node->pkt->pkttype == PKT_SECRET_SUBKEY )
253 keyid_from_pk (node->pkt->pkt.public_key, kid);
255 for (; list; list = list->next)
256 if (list->kid[0] == kid[0] && list->kid[1] == kid[1])
262 /* Allocate a new subkey list item from NODE. */
264 new_subkey_list_item (KBNODE node)
266 subkey_list_t list = xcalloc (1, sizeof *list);
268 if (node->pkt->pkttype == PKT_PUBLIC_SUBKEY
269 || node->pkt->pkttype == PKT_SECRET_SUBKEY)
270 keyid_from_pk (node->pkt->pkt.public_key, list->kid);
276 /* Helper function to check whether the subkey at NODE actually
277 matches the description at DESC. The function returns true if the
278 key under question has been specified by an exact specification
279 (keyID or fingerprint) and does match the one at NODE. It is
280 assumed that the packet at NODE is either a public or secret
283 exact_subkey_match_p (KEYDB_SEARCH_DESC *desc, KBNODE node)
286 byte fpr[MAX_FINGERPRINT_LEN];
292 case KEYDB_SEARCH_MODE_SHORT_KID:
293 case KEYDB_SEARCH_MODE_LONG_KID:
294 keyid_from_pk (node->pkt->pkt.public_key, kid);
297 case KEYDB_SEARCH_MODE_FPR16:
298 case KEYDB_SEARCH_MODE_FPR20:
299 case KEYDB_SEARCH_MODE_FPR:
300 fingerprint_from_pk (node->pkt->pkt.public_key, fpr,&fprlen);
309 case KEYDB_SEARCH_MODE_SHORT_KID:
310 if (desc->u.kid[1] == kid[1])
314 case KEYDB_SEARCH_MODE_LONG_KID:
315 if (desc->u.kid[0] == kid[0] && desc->u.kid[1] == kid[1])
319 case KEYDB_SEARCH_MODE_FPR16:
320 if (!memcmp (desc->u.fpr, fpr, 16))
324 case KEYDB_SEARCH_MODE_FPR20:
325 case KEYDB_SEARCH_MODE_FPR:
326 if (!memcmp (desc->u.fpr, fpr, 20))
338 /* Return a canonicalized public key algoithms. This is used to
339 compare different flavors of algorithms (e.g. ELG and ELG_E are
340 considered the same). */
342 canon_pubkey_algo (int algo)
348 case GCRY_PK_RSA_S: return GCRY_PK_RSA;
350 case GCRY_PK_ELG_E: return GCRY_PK_ELG;
351 default: return algo;
356 /* Use the key transfer format given in S_PGP to create the secinfo
357 structure in PK and change the parameter array in PK to include the
358 secret parameters. */
360 transfer_format_to_openpgp (gcry_sexp_t s_pgp, PKT_public_key *pk)
363 gcry_sexp_t top_list;
364 gcry_sexp_t list = NULL;
369 int is_v4, is_protected;
371 int protect_algo = 0;
379 gcry_mpi_t skey[10]; /* We support up to 9 parameters. */
381 struct seckey_info *ski;
383 top_list = gcry_sexp_find_token (s_pgp, "openpgp-private-key", 0);
387 list = gcry_sexp_find_token (top_list, "version", 0);
390 value = gcry_sexp_nth_data (list, 1, &valuelen);
391 if (!value || valuelen != 1 || !(value[0] == '3' || value[0] == '4'))
393 is_v4 = (value[0] == '4');
395 gcry_sexp_release (list);
396 list = gcry_sexp_find_token (top_list, "protection", 0);
399 value = gcry_sexp_nth_data (list, 1, &valuelen);
402 if (valuelen == 4 && !memcmp (value, "sha1", 4))
404 else if (valuelen == 3 && !memcmp (value, "sum", 3))
406 else if (valuelen == 4 && !memcmp (value, "none", 4))
412 string = gcry_sexp_nth_string (list, 2);
415 protect_algo = gcry_cipher_map_name (string);
418 value = gcry_sexp_nth_data (list, 3, &valuelen);
419 if (!value || !valuelen || valuelen > sizeof iv)
421 memcpy (iv, value, valuelen);
424 string = gcry_sexp_nth_string (list, 4);
427 s2k_mode = strtol (string, NULL, 10);
430 string = gcry_sexp_nth_string (list, 5);
433 s2k_algo = gcry_md_map_name (string);
436 value = gcry_sexp_nth_data (list, 6, &valuelen);
437 if (!value || !valuelen || valuelen > sizeof s2k_salt)
439 memcpy (s2k_salt, value, valuelen);
441 string = gcry_sexp_nth_string (list, 7);
444 s2k_count = strtoul (string, NULL, 10);
448 gcry_sexp_release (list);
449 list = gcry_sexp_find_token (top_list, "algo", 0);
452 string = gcry_sexp_nth_string (list, 1);
455 pubkey_algo = gcry_pk_map_name (string);
458 if (gcry_pk_algo_info (pubkey_algo, GCRYCTL_GET_ALGO_NPKEY, NULL, &npkey)
459 || gcry_pk_algo_info (pubkey_algo, GCRYCTL_GET_ALGO_NSKEY, NULL, &nskey)
460 || !npkey || npkey >= nskey || nskey > PUBKEY_MAX_NSKEY)
462 pubkey_algo = map_pk_gcry_to_openpgp (pubkey_algo);
464 gcry_sexp_release (list);
465 list = gcry_sexp_find_token (top_list, "skey", 0);
472 value = gcry_sexp_nth_data (list, ++idx, &valuelen);
473 if (!value && skeyidx >= npkey)
476 /* Check for too many parameters. Note that depending on the
477 protection mode and version number we may see less than NSKEY
478 (but at least NPKEY+1) parameters. */
481 if (skeyidx >= DIM (skey)-1)
484 if (!value || valuelen != 1 || !(value[0] == '_' || value[0] == 'e'))
486 is_enc = (value[0] == 'e');
487 value = gcry_sexp_nth_data (list, ++idx, &valuelen);
488 if (!value || !valuelen)
492 void *p = xtrymalloc (valuelen);
495 memcpy (p, value, valuelen);
496 skey[skeyidx] = gcry_mpi_set_opaque (NULL, p, valuelen*8);
502 if (gcry_mpi_scan (skey + skeyidx, GCRYMPI_FMT_STD,
503 value, valuelen, NULL))
508 skey[skeyidx++] = NULL;
510 gcry_sexp_release (list);
512 /* We have no need for the CSUM valuel thus we don't parse it. */
513 /* list = gcry_sexp_find_token (top_list, "csum", 0); */
516 /* string = gcry_sexp_nth_string (list, 1); */
518 /* goto bad_seckey; */
519 /* desired_csum = strtoul (string, NULL, 10); */
520 /* xfree (string); */
523 /* desired_csum = 0; */
524 /* gcry_sexp_release (list); list = NULL; */
526 gcry_sexp_release (top_list); top_list = NULL;
528 /* log_debug ("XXX is_v4=%d\n", is_v4); */
529 /* log_debug ("XXX pubkey_algo=%d\n", pubkey_algo); */
530 /* log_debug ("XXX is_protected=%d\n", is_protected); */
531 /* log_debug ("XXX protect_algo=%d\n", protect_algo); */
532 /* log_printhex ("XXX iv", iv, ivlen); */
533 /* log_debug ("XXX ivlen=%d\n", ivlen); */
534 /* log_debug ("XXX s2k_mode=%d\n", s2k_mode); */
535 /* log_debug ("XXX s2k_algo=%d\n", s2k_algo); */
536 /* log_printhex ("XXX s2k_salt", s2k_salt, sizeof s2k_salt); */
537 /* log_debug ("XXX s2k_count=%lu\n", (unsigned long)s2k_count); */
538 /* for (idx=0; skey[idx]; idx++) */
540 /* int is_enc = gcry_mpi_get_flag (skey[idx], GCRYMPI_FLAG_OPAQUE); */
541 /* log_info ("XXX skey[%d]%s:", idx, is_enc? " (enc)":""); */
545 /* unsigned int nbits; */
546 /* p = gcry_mpi_get_opaque (skey[idx], &nbits); */
547 /* log_printhex (NULL, p, (nbits+7)/8); */
550 /* gcry_mpi_dump (skey[idx]); */
551 /* log_printf ("\n"); */
554 if (!is_v4 || is_protected != 2 )
556 /* We only support the v4 format and a SHA-1 checksum. */
557 err = gpg_error (GPG_ERR_NOT_IMPLEMENTED);
561 /* We need to change the received parameters for ECC algorithms.
562 The transfer format has all parameters but OpenPGP defines that
563 only the OID of the curve is to be used. */
564 if (pubkey_algo == PUBKEY_ALGO_ECDSA || pubkey_algo == PUBKEY_ALGO_ECDH)
566 gcry_sexp_t s_pubkey;
567 const char *curvename, *curveoidstr;
570 /* We build an S-expression with the public key parameters and
571 ask Libgcrypt to return the matching curve name. */
572 if (npkey != 6 || !skey[0] || !skey[1] || !skey[2]
573 || !skey[3] || !skey[4] || !skey[5]
574 || !skey[6] || skey[7])
576 err = gpg_error (GPG_ERR_INTERNAL);
579 err = gcry_sexp_build (&s_pubkey, NULL,
580 "(public-key(ecc(p%m)(a%m)(b%m)(g%m)(n%m)))",
581 skey[0], skey[1], skey[2], skey[3], skey[4]);
584 curvename = gcry_pk_get_curve (s_pubkey, 0, NULL);
585 gcry_sexp_release (s_pubkey);
586 curveoidstr = gpg_curve_to_oid (curvename, NULL);
589 log_error ("no OID known for curve `%s'\n", curvename);
590 err = gpg_error (GPG_ERR_UNKNOWN_NAME);
593 err = openpgp_oid_from_str (curveoidstr, &mpi);
597 /* Now replace the curve parameters by the OID and shift the
598 rest of the parameters. */
599 gcry_mpi_release (skey[0]);
601 for (idx=1; idx <= 4; idx++)
602 gcry_mpi_release (skey[idx]);
605 for (idx=3; idx <= 6; idx++)
608 /* Fixup the NPKEY and NSKEY to match OpenPGP reality. */
612 /* for (idx=0; skey[idx]; idx++) */
614 /* log_info ("YYY skey[%d]:", idx); */
615 /* if (gcry_mpi_get_flag (skey[idx], GCRYMPI_FLAG_OPAQUE)) */
618 /* unsigned int nbits; */
619 /* p = gcry_mpi_get_opaque (skey[idx], &nbits); */
620 /* log_printhex (NULL, p, (nbits+7)/8); */
623 /* gcry_mpi_dump (skey[idx]); */
624 /* log_printf ("\n"); */
628 /* Do some sanity checks. */
631 /* We expect an already encoded S2K count. */
632 err = gpg_error (GPG_ERR_INV_DATA);
635 if (canon_pubkey_algo (pubkey_algo) != canon_pubkey_algo (pk->pubkey_algo))
637 err = gpg_error (GPG_ERR_PUBKEY_ALGO);
640 err = openpgp_cipher_test_algo (protect_algo);
643 err = openpgp_md_test_algo (s2k_algo);
647 /* Check that the public key parameters match. Note that since
648 Libgcrypt 1.5 gcry_mpi_cmp handles opaque MPI correctly. */
649 for (idx=0; idx < npkey; idx++)
650 if (gcry_mpi_cmp (pk->pkey[idx], skey[idx]))
652 err = gpg_error (GPG_ERR_BAD_PUBKEY);
656 /* Check that the first secret key parameter in SKEY is encrypted
657 and that there are no more secret key parameters. The latter is
658 guaranteed by the v4 packet format. */
659 if (!gcry_mpi_get_flag (skey[npkey], GCRYMPI_FLAG_OPAQUE))
661 if (npkey+1 < DIM (skey) && skey[npkey+1])
664 /* Check that the secret key parameters in PK are all set to NULL. */
665 for (idx=npkey; idx < nskey; idx++)
669 /* Now build the protection info. */
670 pk->seckey_info = ski = xtrycalloc (1, sizeof *ski);
673 err = gpg_error_from_syserror ();
677 ski->is_protected = 1;
679 ski->algo = protect_algo;
680 ski->s2k.mode = s2k_mode;
681 ski->s2k.hash_algo = s2k_algo;
682 assert (sizeof ski->s2k.salt == sizeof s2k_salt);
683 memcpy (ski->s2k.salt, s2k_salt, sizeof s2k_salt);
684 ski->s2k.count = s2k_count;
685 assert (ivlen <= sizeof ski->iv);
686 memcpy (ski->iv, iv, ivlen);
689 /* Store the protected secret key parameter. */
690 pk->pkey[npkey] = skey[npkey];
696 gcry_sexp_release (list);
697 gcry_sexp_release (top_list);
698 for (idx=0; idx < skeyidx; idx++)
699 gcry_mpi_release (skey[idx]);
703 err = gpg_error (GPG_ERR_BAD_SECKEY);
707 err = gpg_error (GPG_ERR_ENOMEM);
711 /* Export the keys identified by the list of strings in USERS to the
712 stream OUT. If Secret is false public keys will be exported. With
713 secret true secret keys will be exported; in this case 1 means the
714 entire secret keyblock and 2 only the subkeys. OPTIONS are the
715 export options to apply. If KEYBLOCK_OUT is not NULL, AND the exit
716 code is zero, a pointer to the first keyblock found and exported
717 will be stored at this address; no other keyblocks are exported in
718 this case. The caller must free it the returned keyblock. If any
719 key has been exported true is stored at ANY. */
721 do_export_stream (ctrl_t ctrl, iobuf_t out, strlist_t users, int secret,
722 kbnode_t *keyblock_out, unsigned int options, int *any)
726 KBNODE keyblock = NULL;
728 size_t ndesc, descindex;
729 KEYDB_SEARCH_DESC *desc = NULL;
730 subkey_list_t subkey_list = NULL; /* Track already processed subkeys. */
734 gcry_cipher_hd_t cipherhd = NULL;
738 kdbhd = keydb_new ();
743 desc = xcalloc (ndesc, sizeof *desc);
744 desc[0].mode = KEYDB_SEARCH_MODE_FIRST;
748 for (ndesc=0, sl=users; sl; sl = sl->next, ndesc++)
750 desc = xmalloc ( ndesc * sizeof *desc);
752 for (ndesc=0, sl=users; sl; sl = sl->next)
754 if (!(err=classify_user_id (sl->d, desc+ndesc, 1)))
757 log_error (_("key \"%s\" not found: %s\n"),
758 sl->d, gpg_strerror (err));
761 /* It would be nice to see which of the given users did actually
762 match one in the keyring. To implement this we need to have
763 a found flag for each entry in desc. To set this flag we
764 must check all those entries after a match to mark all
765 matched one - currently we stop at the first match. To do
766 this we need an extra flag to enable this feature. */
769 #ifdef ENABLE_SELINUX_HACKS
772 log_error (_("exporting secret keys not allowed\n"));
773 err = gpg_error (GPG_ERR_NOT_SUPPORTED);
778 /* For secret key export we need to setup a decryption context. */
784 err = agent_keywrap_key (ctrl, 1, &kek, &keklen);
787 log_error ("error getting the KEK: %s\n", gpg_strerror (err));
791 /* Prepare a cipher context. */
792 err = gcry_cipher_open (&cipherhd, GCRY_CIPHER_AES128,
793 GCRY_CIPHER_MODE_AESWRAP, 0);
795 err = gcry_cipher_setkey (cipherhd, kek, keklen);
798 log_error ("error setting up an encryption context: %s\n",
806 while (!(err = keydb_search2 (kdbhd, desc, ndesc, &descindex)))
808 int skip_until_subkey = 0;
813 desc[0].mode = KEYDB_SEARCH_MODE_NEXT;
815 /* Read the keyblock. */
816 release_kbnode (keyblock);
818 err = keydb_get_keyblock (kdbhd, &keyblock);
821 log_error (_("error reading keyblock: %s\n"), gpg_strerror (err));
825 node = find_kbnode (keyblock, PKT_PUBLIC_KEY);
828 log_error ("public key packet not found in keyblock - skipped\n");
831 pk = node->pkt->pkt.public_key;
832 keyid_from_pk (pk, keyid);
834 /* If a secret key export is required we need to check whether
835 we have a secret key at all and if so create the seckey_info
839 if (agent_probe_any_secret_key (ctrl, keyblock))
840 continue; /* No secret key (neither primary nor subkey). */
842 /* No v3 keys with GNU mode 1001. */
843 if (secret == 2 && pk->version == 3)
845 log_info (_("key %s: PGP 2.x style key - skipped\n"),
850 /* The agent does not yet allow to export v3 packets. It is
851 actually questionable whether we should allow them at
853 if (pk->version == 3)
855 log_info ("key %s: PGP 2.x style key (v3) export "
856 "not yet supported - skipped\n", keystr (keyid));
861 /* Always do the cleaning on the public key part if requested.
862 Note that we don't yet set this option if we are exporting
863 secret keys. Note that both export-clean and export-minimal
864 only apply to UID sigs (0x10, 0x11, 0x12, and 0x13). A
865 designated revocation is never stripped, even with
866 export-minimal set. */
867 if ((options & EXPORT_CLEAN))
868 clean_key (keyblock, opt.verbose, (options&EXPORT_MINIMAL), NULL, NULL);
871 for (kbctx=NULL; (node = walk_kbnode (keyblock, &kbctx, 0)); )
873 if (skip_until_subkey)
875 if (node->pkt->pkttype == PKT_PUBLIC_SUBKEY)
876 skip_until_subkey = 0;
881 /* We used to use comment packets, but not any longer. In
882 case we still have comments on a key, strip them here
883 before we call build_packet(). */
884 if (node->pkt->pkttype == PKT_COMMENT)
887 /* Make sure that ring_trust packets never get exported. */
888 if (node->pkt->pkttype == PKT_RING_TRUST)
891 /* If exact is set, then we only export what was requested
892 (plus the primary key, if the user didn't specifically
894 if (desc[descindex].exact
895 && node->pkt->pkttype == PKT_PUBLIC_SUBKEY)
897 if (!exact_subkey_match_p (desc+descindex, node))
899 /* Before skipping this subkey, check whether any
900 other description wants an exact match on a
901 subkey and include that subkey into the output
902 too. Need to add this subkey to a list so that
903 it won't get processed a second time.
905 So the first step here is to check that list and
906 skip in any case if the key is in that list.
908 We need this whole mess because the import
909 function of GnuPG < 2.1 is not able to merge
910 secret keys and thus it is useless to output them
911 as two separate keys and have import merge them. */
912 if (subkey_in_list_p (subkey_list, node))
913 skip_until_subkey = 1; /* Already processed this one. */
918 for (j=0; j < ndesc; j++)
919 if (j != descindex && desc[j].exact
920 && exact_subkey_match_p (desc+j, node))
923 skip_until_subkey = 1; /* No other one matching. */
927 if(skip_until_subkey)
930 /* Mark this one as processed. */
932 subkey_list_t tmp = new_subkey_list_item (node);
933 tmp->next = subkey_list;
938 if (node->pkt->pkttype == PKT_SIGNATURE)
940 /* Do not export packets which are marked as not
942 if (!(options&EXPORT_LOCAL_SIGS)
943 && !node->pkt->pkt.signature->flags.exportable)
944 continue; /* not exportable */
946 /* Do not export packets with a "sensitive" revocation
947 key unless the user wants us to. Note that we do
948 export these when issuing the actual revocation
950 if (!(options&EXPORT_SENSITIVE_REVKEYS)
951 && node->pkt->pkt.signature->revkey)
955 for (i=0;i<node->pkt->pkt.signature->numrevkeys;i++)
956 if ( (node->pkt->pkt.signature->revkey[i]->class & 0x40))
959 if (i < node->pkt->pkt.signature->numrevkeys)
964 /* Don't export attribs? */
965 if (!(options&EXPORT_ATTRIBUTES)
966 && node->pkt->pkttype == PKT_USER_ID
967 && node->pkt->pkt.user_id->attrib_data )
969 /* Skip until we get to something that is not an attrib
970 or a signature on an attrib */
971 while (kbctx->next && kbctx->next->pkt->pkttype==PKT_SIGNATURE)
977 if (secret && (node->pkt->pkttype == PKT_PUBLIC_KEY
978 || node->pkt->pkttype == PKT_PUBLIC_SUBKEY))
980 u32 subkidbuf[2], *subkid;
981 char *hexgrip, *serialno;
983 pk = node->pkt->pkt.public_key;
984 if (node->pkt->pkttype == PKT_PUBLIC_KEY)
988 keyid_from_pk (pk, subkidbuf);
994 log_error ("key %s: oops: seckey_info already set"
995 " - skipped\n", keystr_with_sub (keyid, subkid));
996 skip_until_subkey = 1;
1000 err = hexkeygrip_from_pk (pk, &hexgrip);
1003 log_error ("key %s: error computing keygrip: %s"
1004 " - skipped\n", keystr_with_sub (keyid, subkid),
1005 gpg_strerror (err));
1006 skip_until_subkey = 1;
1011 if (secret == 2 && node->pkt->pkttype == PKT_PUBLIC_KEY)
1013 /* We are asked not to export the secret parts of
1014 the primary key. Make up an error code to create
1016 err = GPG_ERR_NOT_FOUND;
1020 err = agent_get_keyinfo (ctrl, hexgrip, &serialno);
1022 if ((!err && serialno)
1023 && secret == 2 && node->pkt->pkttype == PKT_PUBLIC_KEY)
1025 /* It does not make sense to export a key with its
1026 primary key on card using a non-key stub. Thus
1027 we skip those keys when used with
1028 --export-secret-subkeys. */
1029 log_info (_("key %s: key material on-card - skipped\n"),
1030 keystr_with_sub (keyid, subkid));
1031 skip_until_subkey = 1;
1033 else if (gpg_err_code (err) == GPG_ERR_NOT_FOUND
1034 || (!err && serialno))
1036 /* Create a key stub. */
1037 struct seckey_info *ski;
1040 pk->seckey_info = ski = xtrycalloc (1, sizeof *ski);
1043 err = gpg_error_from_syserror ();
1048 ski->is_protected = 1;
1050 ski->s2k.mode = 1001; /* GNU dummy (no secret key). */
1053 ski->s2k.mode = 1002; /* GNU-divert-to-card. */
1054 for (s=serialno; sizeof (ski->ivlen) && *s && s[1];
1055 ski->ivlen++, s += 2)
1056 ski->iv[ski->ivlen] = xtoi_2 (s);
1059 if ((options&EXPORT_SEXP_FORMAT))
1060 err = build_sexp (out, node->pkt, &indent);
1062 err = build_packet (out, node->pkt);
1066 /* FIXME: Move this spaghetti code into a separate
1068 unsigned char *wrappedkey = NULL;
1069 size_t wrappedkeylen;
1070 unsigned char *key = NULL;
1071 size_t keylen, realkeylen;
1075 log_info ("key %s: asking agent for the secret parts\n",
1076 keystr_with_sub (keyid, subkid));
1078 err = agent_export_key (ctrl, hexgrip, "Key foo", NULL,
1079 &wrappedkey, &wrappedkeylen);
1082 if (wrappedkeylen < 24)
1084 err = gpg_error (GPG_ERR_INV_LENGTH);
1087 keylen = wrappedkeylen - 8;
1088 key = xtrymalloc_secure (keylen);
1091 err = gpg_error_from_syserror ();
1094 err = gcry_cipher_decrypt (cipherhd, key, keylen,
1095 wrappedkey, wrappedkeylen);
1098 realkeylen = gcry_sexp_canon_len (key, keylen, NULL, &err);
1100 goto unwraperror; /* Invalid csexp. */
1102 err = gcry_sexp_sscan (&s_skey, NULL, key, realkeylen);
1107 err = transfer_format_to_openpgp (s_skey, pk);
1108 gcry_sexp_release (s_skey);
1112 if ((options&EXPORT_SEXP_FORMAT))
1113 err = build_sexp (out, node->pkt, &indent);
1115 err = build_packet (out, node->pkt);
1116 goto unwraperror_leave;
1123 log_error ("key %s: error receiving key from agent:"
1125 keystr_with_sub (keyid, subkid),
1127 gpg_err_code (err) == GPG_ERR_FULLY_CANCELED?
1128 "":_(" - skipped"));
1129 if (gpg_err_code (err) == GPG_ERR_FULLY_CANCELED)
1131 skip_until_subkey = 1;
1139 log_error ("key %s: error getting keyinfo from agent: %s"
1140 " - skipped\n", keystr_with_sub (keyid, subkid),
1141 gpg_strerror (err));
1142 skip_until_subkey = 1;
1146 xfree (pk->seckey_info);
1147 pk->seckey_info = NULL;
1152 if ((options&EXPORT_SEXP_FORMAT))
1153 err = build_sexp (out, node->pkt, &indent);
1155 err = build_packet (out, node->pkt);
1160 log_error ("build_packet(%d) failed: %s\n",
1161 node->pkt->pkttype, gpg_strerror (err));
1165 if (!skip_until_subkey)
1169 if ((options&EXPORT_SEXP_FORMAT) && indent)
1171 for (; indent; indent--)
1172 iobuf_put (out, ')');
1173 iobuf_put (out, '\n');
1178 *keyblock_out = keyblock;
1182 if ((options&EXPORT_SEXP_FORMAT) && indent)
1184 for (; indent; indent--)
1185 iobuf_put (out, ')');
1186 iobuf_put (out, '\n');
1188 if (gpg_err_code (err) == GPG_ERR_NOT_FOUND)
1192 gcry_cipher_close (cipherhd);
1193 release_subkey_list (subkey_list);
1195 keydb_release (kdbhd);
1196 if (err || !keyblock_out)
1197 release_kbnode( keyblock );
1199 log_info(_("WARNING: nothing exported\n"));
1206 /* write_sexp_line (iobuf_t out, int *indent, const char *text) */
1210 /* for (i=0; i < *indent; i++) */
1211 /* iobuf_put (out, ' '); */
1212 /* iobuf_writestr (out, text); */
1217 /* write_sexp_keyparm (iobuf_t out, int *indent, const char *name, gcry_mpi_t a) */
1220 /* unsigned char *buffer; */
1222 /* write_sexp_line (out, indent, "("); */
1223 /* iobuf_writestr (out, name); */
1224 /* iobuf_writestr (out, " #"); */
1226 /* rc = gcry_mpi_aprint (GCRYMPI_FMT_HEX, &buffer, NULL, a); */
1228 /* iobuf_writestr (out, buffer); */
1229 /* iobuf_writestr (out, "#)"); */
1230 /* gcry_free (buffer); */
1235 build_sexp_seckey (iobuf_t out, PACKET *pkt, int *indent)
1241 /* FIXME: Not yet implemented. */
1242 return gpg_error (GPG_ERR_NOT_IMPLEMENTED);
1243 /* PKT_secret_key *sk = pkt->pkt.secret_key; */
1244 /* char tmpbuf[100]; */
1246 /* if (pkt->pkttype == PKT_SECRET_KEY) */
1248 /* iobuf_writestr (out, "(openpgp-key\n"); */
1253 /* iobuf_writestr (out, " (subkey\n"); */
1257 /* write_sexp_line (out, indent, "(private-key\n"); */
1259 /* if (is_RSA (sk->pubkey_algo) && !sk->is_protected) */
1261 /* write_sexp_line (out, indent, "(rsa\n"); */
1263 /* write_sexp_keyparm (out, indent, "n", sk->skey[0]); iobuf_put (out,'\n'); */
1264 /* write_sexp_keyparm (out, indent, "e", sk->skey[1]); iobuf_put (out,'\n'); */
1265 /* write_sexp_keyparm (out, indent, "d", sk->skey[2]); iobuf_put (out,'\n'); */
1266 /* write_sexp_keyparm (out, indent, "p", sk->skey[3]); iobuf_put (out,'\n'); */
1267 /* write_sexp_keyparm (out, indent, "q", sk->skey[4]); iobuf_put (out,'\n'); */
1268 /* write_sexp_keyparm (out, indent, "u", sk->skey[5]); */
1269 /* iobuf_put (out,')'); iobuf_put (out,'\n'); */
1272 /* else if (sk->pubkey_algo == PUBKEY_ALGO_DSA && !sk->is_protected) */
1274 /* write_sexp_line (out, indent, "(dsa\n"); */
1276 /* write_sexp_keyparm (out, indent, "p", sk->skey[0]); iobuf_put (out,'\n'); */
1277 /* write_sexp_keyparm (out, indent, "q", sk->skey[1]); iobuf_put (out,'\n'); */
1278 /* write_sexp_keyparm (out, indent, "g", sk->skey[2]); iobuf_put (out,'\n'); */
1279 /* write_sexp_keyparm (out, indent, "y", sk->skey[3]); iobuf_put (out,'\n'); */
1280 /* write_sexp_keyparm (out, indent, "x", sk->skey[4]); */
1281 /* iobuf_put (out,')'); iobuf_put (out,'\n'); */
1284 /* else if (sk->pubkey_algo == PUBKEY_ALGO_ECDSA && !sk->is_protected) */
1286 /* write_sexp_line (out, indent, "(ecdsa\n"); */
1288 /* write_sexp_keyparm (out, indent, "c", sk->skey[0]); iobuf_put (out,'\n'); */
1289 /* write_sexp_keyparm (out, indent, "q", sk->skey[6]); iobuf_put (out,'\n'); */
1290 /* write_sexp_keyparm (out, indent, "d", sk->skey[7]); */
1291 /* iobuf_put (out,')'); iobuf_put (out,'\n'); */
1294 /* else if (is_ELGAMAL (sk->pubkey_algo) && !sk->is_protected) */
1296 /* write_sexp_line (out, indent, "(elg\n"); */
1298 /* write_sexp_keyparm (out, indent, "p", sk->skey[0]); iobuf_put (out,'\n'); */
1299 /* write_sexp_keyparm (out, indent, "g", sk->skey[2]); iobuf_put (out,'\n'); */
1300 /* write_sexp_keyparm (out, indent, "y", sk->skey[3]); iobuf_put (out,'\n'); */
1301 /* write_sexp_keyparm (out, indent, "x", sk->skey[4]); */
1302 /* iobuf_put (out,')'); iobuf_put (out,'\n'); */
1305 /* write_sexp_line (out, indent, "(attrib\n"); (*indent)++; */
1306 /* sprintf (tmpbuf, "(created \"%lu\"", (unsigned long)sk->timestamp); */
1307 /* write_sexp_line (out, indent, tmpbuf); */
1308 /* iobuf_put (out,')'); (*indent)--; /\* close created *\/ */
1309 /* iobuf_put (out,')'); (*indent)--; /\* close attrib *\/ */
1310 /* iobuf_put (out,')'); (*indent)--; /\* close private-key *\/ */
1311 /* if (pkt->pkttype != PKT_SECRET_KEY) */
1312 /* iobuf_put (out,')'), (*indent)--; /\* close subkey *\/ */
1313 /* iobuf_put (out,'\n'); */
1319 /* For some packet types we write them in a S-expression format. This
1320 is still EXPERIMENTAL and subject to change. */
1322 build_sexp (iobuf_t out, PACKET *pkt, int *indent)
1326 switch (pkt->pkttype)
1328 case PKT_SECRET_KEY:
1329 case PKT_SECRET_SUBKEY:
1330 rc = build_sexp_seckey (out, pkt, indent);